OSDev.org

The Place to Start for Operating System Developers
It is currently Thu Mar 28, 2024 3:11 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: qemu crashes when asm is called
PostPosted: Fri Jan 11, 2019 9:45 am 
Offline

Joined: Fri Jan 11, 2019 7:59 am
Posts: 6
Sorry if I'm in the wrong sub section, I'm new.

I was just following the steps on this OS-Tutorial but I encountered into a problem, calling the assembly function in C like
Code:
asm volatile("sti")
crashes qemu for some reason. Can anyone tell me how to fix this?

Tutorial I was following:
https://github.com/cfenollosa/os-tutori ... upts-timer

Error from qemu:
Code:
qemu-system-i386: Trying to execute code outside RAM or ROM at 0x6a006afa
This usually means one of the following happened:

(1) You told QEMU to execute a kernel for the wrong machine type, and it crashed on startup (eg trying to run a raspberry pi kernel on a versatilepb QEMU machine)
(2) You didn't give QEMU a kernel or BIOS filename at all, and QEMU executed a ROM full of no-op instructions until it fell off the end
(3) Your guest kernel has a bug and crashed by jumping off into nowhere

This is almost always one of the first two, so check your command line and that you are using the right type of kernel for this machine.
If you think option (3) is likely then you can try debugging your guest with the -d debug options; in particular -d guest_errors will cause the log to include a dump of the guest register state at this point.

Execution cannot continue; stopping here.

make: *** [makefile:26: run] Error 1


qemu-system-i386 -d guest_errors -fda Ranedeer.bin:
Code:
qemu: fatal: Trying to execute code outside RAM or ROM at 0x6a006afa
EAX=00000034 EBX=00000000 ECX=000027e6 EDX=00000040
ESI=00000000 EDI=00000000 EBP=0008ffd0 ESP=0008ff9c
EIP=6a006afa EFL=00000057 [---ZAPC] CPL=0 II=0 A20=1 SMM=0 HLT=0
ES =0010 00000000 ffffffff 00cf9300 DPL=0 DS   [-WA]
CS =0008 00000000 ffffffff 00cf9a00 DPL=0 CS32 [-R-]
SS =0010 00000000 ffffffff 00cf9300 DPL=0 DS   [-WA]
DS =0010 00000000 ffffffff 00cf9300 DPL=0 DS   [-WA]
FS =0010 00000000 ffffffff 00cf9300 DPL=0 DS   [-WA]
GS =0010 00000000 ffffffff 00cf9300 DPL=0 DS   [-WA]
LDT=0000 00000000 0000ffff 00008200 DPL=0 LDT
TR =0000 00000000 0000ffff 00008b00 DPL=0 TSS32-busy
GDT=     00007ccd 00000017
IDT=     00000000 000007ff
CR0=00000011 CR2=00000000 CR3=00000000 CR4=00000000
DR0=00000000 DR1=00000000 DR2=00000000 DR3=00000000
DR6=ffff0ff0 DR7=00000400
CCS=ffffdf41 CCD=00000000 CCO=ADDL   
EFER=0000000000000000
FCW=037f FSW=0000 [ST=0] FTW=00 MXCSR=00001f80
FPR0=0000000000000000 0000 FPR1=0000000000000000 0000
FPR2=0000000000000000 0000 FPR3=0000000000000000 0000
FPR4=0000000000000000 0000 FPR5=0000000000000000 0000
FPR6=0000000000000000 0000 FPR7=0000000000000000 0000
XMM00=00000000000000000000000000000000 XMM01=00000000000000000000000000000000
XMM02=00000000000000000000000000000000 XMM03=00000000000000000000000000000000
XMM04=00000000000000000000000000000000 XMM05=00000000000000000000000000000000
XMM06=00000000000000000000000000000000 XMM07=00000000000000000000000000000000


Top
 Profile  
 
 Post subject: Re: qemu crashes when asm is called
PostPosted: Fri Jan 11, 2019 10:49 am 
Offline
Member
Member
User avatar

Joined: Sat Mar 31, 2012 3:07 am
Posts: 4591
Location: Chichester, UK
Have you set up an Interrupt Descriptor Table and interrupt handlers? If not, why are you enabling interrupts?


Top
 Profile  
 
 Post subject: Re: qemu crashes when asm is called
PostPosted: Fri Jan 11, 2019 7:08 pm 
Offline

Joined: Fri Jan 11, 2019 7:59 am
Posts: 6
Yes I did, I'm using interrupts for keyboard input.

Here is the isr,idt etc code:
https://github.com/JushBJJ/Ranedeer/tre ... el/lib/CPU


Top
 Profile  
 
 Post subject: Re: qemu crashes when asm is called
PostPosted: Sat Jan 12, 2019 12:59 am 
Offline
Member
Member

Joined: Fri Aug 26, 2016 1:41 pm
Posts: 671
The main problem is that you are not using a cross compiler and your native compiler is producing position independent code that is relying on a global offset table. You tell the linker to ignore the problems, but that just hides the problem. Your IDT will not be referenced properly in memory as a result and things will die a horrible death when STI is issued. I am assuming before you call STI that you also call isr_install(). I highly recommend you use a cross compiler, but if you are intent on using your native compiler get rid of hiding the errors with the linker and compile with -fno-PIE and link (with ld) using -no-pie. This will compile and link as a position independent executable. Your makefile could look like this:
Code:
C_SOURCES = $(wildcard kernel.c lib/*.c lib/Drivers/*.c lib/CPU/*.c)
HEADERS = $(wildcard lib/*.h lib/Drivers/*.h lib/CPU/*.h)
# Nice syntax for file extension replacement
OBJ = ${C_SOURCES:.c=.o lib/CPU/interrupt.o}

# Change this if your cross-compiler is somewhere else
CC = /usr/bin/gcc
GDB = /usr/bin/gdb
# -g: Use debugging symbols in gcc
CFLAGS = -g -m32 -Ilib/ -fno-PIE

# First rule is run by default
Ranedeer.bin: boot/bootsect.bin kernel.bin
        cat $^ > Ranedeer.bin

# '--oformat binary' deletes all symbols as a collateral, so we don't need
# to 'strip' them manually on this case
kernel.bin: boot/kernel_entry.o ${OBJ}
        ld -no-pie -melf_i386 -o $@ -Ttext 0x1000 $^ --oformat binary

# Used for debugging purposes
kernel.elf: boot/kernel_entry.o ${OBJ}
        ld -no-pie -melf_i386  -o $@ -Ttext 0x1000 $^

run: Ranedeer.bin
        qemu-system-i386 -fda Ranedeer.bin

# Open the connection to qemu and load our kernel-object file with symbols
debug: Ranedeer.bin kernel.elf
        qemu-system-i386 -s -fda Ranedeer.bin &
        ${GDB} -ex "target remote localhost:1234" -ex "symbol-file kernel.elf"

# Generic rules for wildcards
# To make an object, always compile from its .c
%.o: %.c ${HEADERS}
        ${CC} ${CFLAGS} -ffreestanding -c $< -o $@

%.o: %.asm
        nasm $< -f elf -o $@

%.bin: %.asm
        nasm $< -f bin -o $@

clean:
        rm -rf *.bin *.dis *.o Ranedeer.bin *.elf
        rm -rf *.o lib/*.o lib/Drivers/*.o Boot/*.o Boot/*.bin lib/CPU/*.o
It should be noted that when running QEMU for debugging you did: qemu-system-i386 -s -fda Ranedeer &.I think it should be qemu-system-i386 -s -fda Ranedeer.bin & given that Ranedeer doesn't include the bootloader. I made that change to your makefile above as well.


Top
 Profile  
 
 Post subject: Re: qemu crashes when asm is called
PostPosted: Sat Jan 12, 2019 7:53 am 
Offline

Joined: Fri Jan 11, 2019 7:59 am
Posts: 6
Man thank you, fixing the makefile a bit fixed the issues, of course i persisted to use the current compilers and linkers that I was currently using.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Bing [Bot] and 58 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group