qemu crashes when reloading segment registers

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
ishkabible
Member
Member
Posts: 37
Joined: Wed Jan 05, 2011 7:35 pm

qemu crashes when reloading segment registers

Post by ishkabible »

so I'm trying to get a "flat" GDT but when I reload the segment registers QEMU just rapidly restarts over and over again. after "mov ds, ax" I can't get anything to execute. if I try "jmp 0x0:.flush" first instead it crashes too. I've checked my code for assigning the correct values to the GDT entrys(which was based on JamesM's kernel development tutorials) and actully used JamesM's tutorial.

I've got this down to what I think is the simplest case that shows my issue. if you need further information please tell me :)

I'm using the following to run my kernel(using multiboot kernel)

Code: Select all

qemu-system-i386 -L bios -kernel kernel.bin
here is my entry point code for multiboot compliance and gdt_flush function

Code: Select all

global loader                           ; making entry point visible to linker
global _gdt_flush
extern _kmain                           ; kmain is defined in kmain.cpp
 
; setting up the Multiboot header - see GRUB docs for details
MODULEALIGN equ  1<<0                   ; align loaded modules on page boundaries
MEMINFO     equ  1<<1                   ; provide memory map
FLAGS       equ  MODULEALIGN | MEMINFO  ; this is the Multiboot 'flag' field
MAGIC       equ  0x1BADB002             ; 'magic number' lets bootloader find the header
CHECKSUM    equ -(MAGIC + FLAGS)        ; checksum required
 
section .text
 
align 4
    dd MAGIC
    dd FLAGS
    dd CHECKSUM
 
; reserve initial kernel stack space
STACKSIZE equ 0x4000                    ; that's 16k.
 
loader:
    mov  esp, stack + STACKSIZE ; set up the stack
    push eax                    ; Multiboot magic number
    push ebx                    ; Multiboot info structure
 
    call _kmain                 ; call kernel proper
 
    cli
.hang:
    hlt                         ; halt machine should kernel return
    jmp  .hang

_gdt_flush:
	mov eax, [esp+4]
	lgdt [eax]
	mov ax, 0x10      ; 0x10 is the offset in the GDT to our data segment
	mov ds, ax       ; crashes here
	mov es, ax
	mov fs, ax
	mov gs, ax
	mov ss, ax
	jmp 0x08:.flush
.flush:
   ret

section .bss
 
align 4
stack: resb STACKSIZE           ; reserve 16k stack on a doubleword boundary
and here is my kernel code

Code: Select all

#include <stdint.h>

struct gdt_entry_struct
{
    uint16_t limit_low;           // The lower 16 bits of the limit.
    uint16_t base_low;            // The lower 16 bits of the base.
    uint8_t  base_middle;         // The next 8 bits of the base.
	uint8_t  access;              // The access byte
	unsigned limit_high : 4;      // upper 4 bits of limit
    unsigned flags      : 4;      // flag bits
    uint8_t  base_high;           // The last 8 bits of the base.
} __attribute__((packed));

typedef struct gdt_entry_struct gdt_entry_t;

struct gdt_ptr_struct
{
    uint16_t limit;            // The upper 16 bits of all selector limits.
    void* base;                // The address of the first gdt_entry_t struct.
} __attribute__((packed));

typedef struct gdt_ptr_struct gdt_ptr_t;

gdt_entry_t gdt_entries[5];
gdt_ptr_t   gdt_ptr;

void gdt_set_gate(uint32_t num, uint32_t base, uint32_t limit, uint8_t access, uint8_t flags)
{
    gdt_entries[num].base_low    = (base & 0xFFFF);
    gdt_entries[num].base_middle = (base >> 16) & 0xFF;
    gdt_entries[num].base_high   = (base >> 24) & 0xFF;

    gdt_entries[num].limit_low   = (limit & 0xFFFF);
    gdt_entries[num].limit_high  = (limit >> 16) & 0xF; 
    
    gdt_entries[num].flags       = flags;
    gdt_entries[num].access      = access;
}

//sets the GDT from assembly
extern gdt_flush(void*);

int init_gdt() {
    gdt_ptr.limit = (sizeof(gdt_entry_t) * 3) - 1;
    gdt_ptr.base  = &gdt_entries;

    gdt_set_gate(0, 0, 0, 0, 0);                // Null segment
    gdt_set_gate(1, 0, 0xFFFFFFFF, 0x9A, 0x00); // Code segment
    gdt_set_gate(2, 0, 0xFFFFFFFF, 0x92, 0x00); // Data segment

	gdt_flush(&gdt_ptr);
}

int kmain(void* mboot_ptr, uint32_t magic) {
	if(magic != 0x2BADB002) while(1);
	init_gdt();
	while(1);
    return 0;
}
Tosi
Member
Member
Posts: 255
Joined: Tue Jun 15, 2010 9:27 am
Location: Flyover State, United States
Contact:

Re: qemu crashes when reloading segment registers

Post by Tosi »

Have you tried running it on Bochs and single stepping?
Or if QEMU has a single-step feature, use that.
User avatar
turdus
Member
Member
Posts: 496
Joined: Tue Feb 08, 2011 1:58 pm

Re: qemu crashes when reloading segment registers

Post by turdus »

I suggest to use bochs with debugger. Write this into your bochsrc: "magic_break: enabled=1", and put "xchg bx,bx" in your code just before the faulting instruction. When bochs starts, debugger prompt will appear (bochs:1>), type "c" and Enter here. When the debugger gets 2nd time called (bochs:2>) issue the following commands and paste results here.

Code: Select all

reg
creg
info gdt
I think you've problems with either your GDT or GDTR, this will help to find out.
ishkabible
Member
Member
Posts: 37
Joined: Wed Jan 05, 2011 7:35 pm

Re: qemu crashes when reloading segment registers

Post by ishkabible »

ok, it restarts in plain bochs too but not repeatedly because I have to do the whole "kernel 200+18...boot" thing.

here's the output from bochsdbg

reg:
rax: 0x00000000_00100010 rcx: 0x00000000_00000000
rdx: 0x00000000_00000092 rbx: 0x00000000_0002bd20
rsp: 0x00000000_00106004 rbp: 0x00000000_00106030
rsi: 0x00000000_0002be78 rdi: 0x00000000_00000080
r8 : 0x00000000_00000000 r9 : 0x00000000_00000000
r10: 0x00000000_00000000 r11: 0x00000000_00000000
r12: 0x00000000_00000000 r13: 0x00000000_00000000
r14: 0x00000000_00000000 r15: 0x00000000_00000000
rip: 0x00000000_0010002a
eflags 0x00000006: id vip vif ac vm rf nt IOPL=0 of df if tf sf zf af PF cf
creg:
CR0=0x60000011: pg CD NW ac wp ne ET ts em mp PE
CR2=page fault laddr=0x0000000000000000
CR3=0x0000000000000000
PCD=page-level cache disable=0
PWT=page-level write-through=0
CR4=0x00000000: smep osxsave pcid fsgsbase smx vmx osxmmexcpt osfxsr pce pge mce
pae pse de tsd pvi vme
EFER=0x00000000: ffxsr nxe lma lme sce
info gdt:
Global Descriptor Table (base=0x0000000000102020, limit=23):
GDT[0x00]=??? descriptor hi=0x00000000, lo=0x00000000
GDT[0x01]=Code segment, base=0x00000000, limit=0x000fffff, Execute/Read, 16-bit
GDT[0x02]=Data segment, base=0x00000000, limit=0x000fffff, Read/Write
You can list individual entries with 'info gdt [NUM]' or groups with 'info gdt [
NUM] [NUM]'
User avatar
DavidCooper
Member
Member
Posts: 1150
Joined: Wed Oct 27, 2010 4:53 pm
Location: Scotland

Re: qemu crashes when reloading segment registers

Post by DavidCooper »

I can't follow what the complicated code does that generates the content of your GDT. Have you tried running it in a program designed to show you what you actually end up with? You probably want the following:-

Code: Select all

00 00 00 00 00 00 00 00
FF FF 00 00 00 9A CF 00
FF FF 00 00 00 92 CF 00
If that isn't what you end up with, what are you getting?
Help the people of Laos by liking - https://www.facebook.com/TheSBInitiative/?ref=py_c

MSB-OS: http://www.magicschoolbook.com/computing/os-project - direct machine code programming
ishkabible
Member
Member
Posts: 37
Joined: Wed Jan 05, 2011 7:35 pm

Re: qemu crashes when reloading segment registers

Post by ishkabible »

I get this.

Code: Select all

00 00 00 00 00 00 00 00
FF FF 00 00 00 A9 F0 00
FF FF 00 00 00 29 F0 00
here's is how I interpret the second line
FFFF (low part of limit)
0000 (low part of base)
0 (middle of base)
0A (access byte)
F (high part of limit)
0 (flags)
00 (high part of base)

I do see 1 issue thanks to that however. I left the flags so that they were in 16-bit mode :/ that dosn't seem to be the issue however. it still crashes even if I make 'flags' 4
User avatar
DavidCooper
Member
Member
Posts: 1150
Joined: Wed Oct 27, 2010 4:53 pm
Location: Scotland

Re: qemu crashes when reloading segment registers

Post by DavidCooper »

Code: Select all

00 00 00 00 00 00 00 00
FF FF 00 00 00 9A CF 00
FF FF 00 00 00 92 CF 00

Code: Select all

00 00 00 00 00 00 00 00
FF FF 00 00 00 A9 F0 00
FF FF 00 00 00 29 F0 00
I can't remember what all the different bytes and bits of bytes are for, but I do know that 9A and 92 work. Your A9 and 29 look as if they're intended to do the same thing, so I suspect you've got them the wrong way round. I'll have to hunt down my old notes on how the GDT works before I can comment on the F0/CF part of it...
Help the people of Laos by liking - https://www.facebook.com/TheSBInitiative/?ref=py_c

MSB-OS: http://www.magicschoolbook.com/computing/os-project - direct machine code programming
User avatar
DavidCooper
Member
Member
Posts: 1150
Joined: Wed Oct 27, 2010 4:53 pm
Location: Scotland

Re: qemu crashes when reloading segment registers

Post by DavidCooper »

You've got the F0 the wrong way round too - it should definitely be 0F. Bits 48 to 51 (the low bits of the seventh byte [byte 6]) are for the limit, while 52 to 55 are for U (use 1 or 0 - doesn't matter which), X (0 - reserved for future function), D/B (op size - 0 for 16-bit or 1 for 32-bit), G (granularity - 0 for 1MB or 1 for 4GB). So, 0F is right for segments covering just a single megabyte, whereas CF is needed for segments covering 4GB.
Help the people of Laos by liking - https://www.facebook.com/TheSBInitiative/?ref=py_c

MSB-OS: http://www.magicschoolbook.com/computing/os-project - direct machine code programming
ishkabible
Member
Member
Posts: 37
Joined: Wed Jan 05, 2011 7:35 pm

Re: qemu crashes when reloading segment registers

Post by ishkabible »

I think it's an endianness issue with printing... not sure how you printed that however

I ran it on windows and printed with this C++ code.

Code: Select all

auto x = reinterpret_cast<char*>(&gdt_entries);
auto e = x + gdt_ptr.limit;
while(x != e) {
std::cout<<std::hex<<(int)(*x & 0xF);
std::cout<<std::hex<<(int)((*x++ >> 4) & 0xF);
}
edit:
yes; it's how I print them, not the actual data. if I take your values and print them from an array. it prints every nibble instead of every byte.

if I fix the printing I get this

00 00 00 00 00 00 00 00
FF FF 00 00 00 9A 0F 00
FF FF 00 00 00 92 0F 00
ishkabible
Member
Member
Posts: 37
Joined: Wed Jan 05, 2011 7:35 pm

Re: qemu crashes when reloading segment registers

Post by ishkabible »

yes, DavidCooper's nailed it!!

so I needed the 32-bit bit set AND the granularity bit set. it's now working!! thanks guys

I'm sure I'll be back later :P
User avatar
DavidCooper
Member
Member
Posts: 1150
Joined: Wed Oct 27, 2010 4:53 pm
Location: Scotland

Re: qemu crashes when reloading segment registers

Post by DavidCooper »

I was about to post this:-

[For the previous byte (byte 5), bit-7 (which is bit-47 of the 8-byte entry) must be 1 (segment present), bits 5 and 6 will both be 0 (privelige level), bit 4 will be 1 for the kernel, so that gives you 9 as the high nibble - that clearly shows that your nibbles must be the wrong way round. The low nibble will have bit-3 as 1 for code or 0 for data, bit-2 as 0 (in most cases), bit-1 as 1 (unless you want code to be unreadable or data to be unwritable, and bit-0 should be 0 (it will turn into a 1 in the GDT automatically as soon as that segment is first used).]

But you were just printing them the wrong way round.

Now I see you say it's working, but it's only a partial success:-

Code: Select all

   mov eax, [esp+4]
   lgdt [eax]
   mov ax, 0x10      ; 0x10 is the offset in the GDT to our data segment
   mov ds, ax       ; crashes here
   mov es, ax
   mov fs, ax
   mov gs, ax
   mov ss, ax
   jmp 0x08:.flush
While looking again at the above section, it occurs to me that you ought to be switching into protected mode (by OR-ing 0x1 with the content of the lowest byte of control register CR0) before you start loading these segment registers with protected mode segments. Your stack will be jumping some distance as soon as you load SS with 0x10.
Help the people of Laos by liking - https://www.facebook.com/TheSBInitiative/?ref=py_c

MSB-OS: http://www.magicschoolbook.com/computing/os-project - direct machine code programming
ishkabible
Member
Member
Posts: 37
Joined: Wed Jan 05, 2011 7:35 pm

Re: qemu crashes when reloading segment registers

Post by ishkabible »

I don't understand why I should switch to protected mode. Is there some documentation or an article I should look at(or perhaps a specific part of the Intel Manuel)?

It should already be in protected mode(GRUB and QEMU should both do that).

I haven't had an issue *yet* but I also haven't done much with it after I thought it was fixed.
User avatar
xenos
Member
Member
Posts: 1144
Joined: Thu Aug 11, 2005 11:00 pm
Libera.chat IRC: xenos1984
Location: Tartu, Estonia
GitHub: https://github.com/xenos1984
Contact:

Re: qemu crashes when reloading segment registers

Post by xenos »

GRUB and QEMU (using the -kernel command line parameter) already switch to protected mode, so there is no need to do that. However, it is recommended that you use your own GDT and reload segment registers as soon as possible. At some later point you will need your own GDT and segment register values anyway (just think of things like TSS or user processes running at PL3, which will need PL3 code and data segments), so it's better to do this first.
Programmers' Hardware Database // GitHub user: xenos1984; OS project: NOS
User avatar
DavidCooper
Member
Member
Posts: 1150
Joined: Wed Oct 27, 2010 4:53 pm
Location: Scotland

Re: qemu crashes when reloading segment registers

Post by DavidCooper »

ishkabible wrote:I don't understand why I should switch to protected mode. Is there some documentation or an article I should look at(or perhaps a specific part of the Intel Manuel)?

It should already be in protected mode(GRUB and QEMU should both do that).

I haven't had an issue *yet* but I also haven't done much with it after I thought it was fixed.
I don't normally get involved when people are booting with GRUB, and I hadn't realised that you were, so what I said about switching into protected mode simply doesn't apply - now I understand why the change you made to the GDT was sufficient to make it work and can see that a whole host of other problems I had envisaged simply won't apply, and what I said about loading SS moving the stack was completely wrong because the processor was already in the right mode to be able to handle the new value going into it.

Good luck with the rest of your project.
Help the people of Laos by liking - https://www.facebook.com/TheSBInitiative/?ref=py_c

MSB-OS: http://www.magicschoolbook.com/computing/os-project - direct machine code programming
ishkabible
Member
Member
Posts: 37
Joined: Wed Jan 05, 2011 7:35 pm

Re: qemu crashes when reloading segment registers

Post by ishkabible »

Thanks, it's actually going to be monotasking, at first at-least, so I think I'll be able to get by with a static GDT. I haven't researched quite enough to see if that will end up being true however.
Post Reply