Problem moving to protected mode...

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
João Jerónimo

Problem moving to protected mode...

Post by João Jerónimo »

After enabling protected mode, with A20 gate already open (don't know if it's important), I reloaded segment registers... it worked well (I used the QEMU command "info registers" to see the values in the Internal Descriptor Cache and they are in agreement with the GDT)...
But when reloading CS (i.e., far jump), QEMU stops responding with no error message... I had to kill it! On real hardware (I used a 486SX, but tomorrow I tell you what appened on my other computer, if needed: 686/AMDAthlon), the CPU just reseted...

Here is the relevant code:
-----------------------------------------------------------------
;CS = 0x1000
;Offsets origin = 0x0000
;Interrupts already disabled...

;Enabling A20 gate
      call A20_enable

;Enabling protected Mode: Loading Global Descriptor Table
      lgdt [cs:GDT_SIZE]
;Enabling protected Mode: Setting PE
      mov eax, cr0
      or eax, 1
      mov cr0, eax
;Now in Protected Mode; Loading Segment Registers
      mov ax, 0x0010
      mov ds, ax
      mov es, ax
      mov fs, ax
      mov gs, ax
      mov ss, ax
[BITS 32]
      jmp 0x8:0x10000+start32
start32:   jmp $




GDT      db   0x00, 0x00, 0x00, 0x00, 0x00, 00000000b, 00000000b, 0x00      ;NULL entry
      db   0xFF, 0xFF, 0x00, 0x00, 0x00, 10011010b, 01001111b, 0x00      ;Kernel Code
      db   0xFF, 0xFF, 0x00, 0x00, 0x00, 10010010b, 01001111b, 0x00      ;Kernel Data

GDT_SIZE   dw   GDT_SIZE-GDT
GDT_BASE   dd   0x10000+GDT
------------------------------------------------------------------------


A not-handled exception, parhaps? It's impossible to handle it now if I can't even load a code segment...
I've tried a bunch of bit patterns in some of the first selector (code) flags, none corrected the problem!

Tks in advance,
Jo?o Jer?nimo
Ryu

Re:Problem moving to protected mode...

Post by Ryu »

I find it insufficent information, but heres the stuff that I find that can be troublesome..

Code: Select all

....
[BITS 32]
      jmp 0x8:0x10000+start32
....
You are suppose to jump before creating 32bit code. I don't know how exactly the assembler is going to assemble the jump instruction, but my guess it would be without a size override prefix and a 32bit offset. In runtime the processor takes the instruction as 16bit code so this can very easily corrupt the rest of the code.

Code: Select all

....
GDT_SIZE   dw   GDT_SIZE-GDT
....
The GDT is a limit not size, just subtract 1 from what you have to make a valid GDT limit. ;)
bluecode

Re:Problem moving to protected mode...

Post by bluecode »

hi,
Jo?o Jer?nimo wrote:

Code: Select all

[BITS 32]
      jmp 0x8:0x10000+start32
       start32:   jmp $
I encountered this problem some time ago, when debugging someone elses code. Ok, the problem is, that the cpu won't be in 32bit mode after your [bits 32]. You have to jump to 32bit protected-mode first. So the cpu will happily execute a 16bit jump, which will be to 0x08:0x00, which obviously isn't where you expect it. I could fix this by just adding a "db 0x67" in front of the jump. This 0x67 is the cpu's address size prefix, which will tell the cpu that the address size of the following instruction is 32bit.
Perhaps it's also possible to move the [Bits 32] after the jmp and to tell your assembler that this is a 32bit jump (perhaps by using "jmpl" or something like this).

Another thing: You perhaps might want to use bochs for debugging ;)

And please use code tags ;)
João Jerónimo

Re:Problem moving to protected mode...

Post by João Jerónimo »

Many thanks to both of you...

Quoting bluecode:
> I encountered this problem some time ago, when debugging
> someone elses code. Ok, the problem is, that the cpu won't be
> in 32bit mode after your [bits 32].

LOL... yes, I know the difference between assembler directives and real processor context! but I thought I had to make a 32-bit jump (with a 32 bit instruction)...
Parhaps i'm telling garbage: I don't understand very much about intruction encodings... but in a pmode tuturial I read while I tried to correct the problem, the author (who used TASM) payed special atention to the far jump... and encoded it directly using declare pseudo-instructions... but for some reason a look at the executable with hexedit made me think my code was correct...

Code: Select all

;The next thing is dirty but there's no way around it:
      db   0eah
      dw   offset start32, code32_idx
> You have to jump to 32bit protected-mode first. So the cpu will
> happily execute a 16bit jump, which will be to 0x08:0x00, which
> obviously isn't where you expect it.

:-)
Jumping to Real-mode code in protected mode doesn't look like a good sport!

> Perhaps it's also possible to move the [Bits 32] after the jmp and
> to tell your assembler that this is a 32bit jump (perhaps by using
> "jmpl" or something like this).

Yes... in Intel syntax I used jmp dword...

> Another thing: You perhaps might want to use bochs for
> debugging ;)

Herm... yes, parhaps...
I've used Bochs a year ago for trying different operating systems... it's a *real* interpretation emulator, many many features, very very sllllloooooow!, so when I knew Qemu, dynamic translation and GPL, I happily threw bochs away...
Yes, I'm considering using bochs... i've not already installed it just because it's summer and my CPU likes to crash while compiling things during summer :-( So i have to point a big fan to it!

> And please use code tags ;)

Ok, I will use them when I need to include code...
But for quotes I don't like to add strange tags... I *do* prefer usenet-style, really!



Quoting Ryu:
> The GDT is a limit not size, just subtract 1 from what you have
> to make a valid GDT limit.

Yes, I've seen code examples that subtracted 1 and others which did not... subtracting one made much more sense for me than not subtracting, but I decided to leave it without the -1 to avoid further problems...
But now it's ok!

Jo?o Jer?nimo
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re:Problem moving to protected mode...

Post by Combuster »

Jo?o Jer?nimo wrote: Parhaps i'm telling garbage: I don't understand very much about intruction encodings... but in a pmode tuturial I read while I tried to correct the problem, the author (who used TASM) payed special atention to the far jump... and encoded it directly using declare pseudo-instructions... but for some reason a look at the executable with hexedit made me think my code was correct...

Code: Select all

;The next thing is dirty but there's no way around it:
      db   0eah
      dw   offset start32, code32_idx
Well, for what i've seen (and used) to enter protected mode, is that there are numerous different approaches:

The protected mode tutorials i have can simply use the 16-bit far jump because they set the code's base in the GDT to the current CS base and not to 0
consequence is the inherent flaw in this code: the linear offset at the top of the file is considered 0x0000, while in fact it is 0x10000. In protected mode suddenly your offset at the top of your file gets the CS added so, while in pmode, you'll have to keep adding 0x10000 to all calls and absolute jumps. With NASM the standard (not considering hacks, the only one) solution is to split the bootloader into stages, since it can not swap between offsets within code during assembly.

what happens here:
your code gets to the jmp. since the pipeline has not yet been flushed it is still executing in 16 bits. the assembler builds an 32-bit opcode, assuming that the processor reaches the point in 32 bit mode, while it does not. It then reads the far jump (byte 1) and then the offset (which is start32 minus the 0x10000 in this case) and then the segment selector: (which is the remainder of the start32 address, 0x1).
Result: jmp far 0x1:start32
Which basically is the null selector

Probably you either want to:
shift [bits 32] down to just above the spinloop, and change the jmp into jmp dword, which will provide the most readable code

otherwise you must do dirty work and add the address size prefix.

On another note: i don't recall bochs as slow. Its just that you should expect not more than 10% of your clockspeed to run decently. on and amdxp/p4 that's a low end pentium. If you compile from source DO enable the optimisations ;)
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
blip

Re:Problem moving to protected mode...

Post by blip »

As a side note, FASM adds the operand-size prefix (0x66) instead of the address-size prefix (0x67) for JMP PWORD 8:0FFFFFFFFh. I have seen code where the author manually assembled the instruction using both prefixes. Immediate operands are treated as plain operands so they require an operand-size prefix to change the size and address-size prefixes are ignored unless there is a memory access going on in the instruction. The jump only changes CS and EIP so there is no memory access generated by it directly, only when the CPU continues execution from the new point does it read new code.
Post Reply