caciquekampeon
caciquekampeon

Reputation: 41

Does switching back to real mode switches back the addessing mode?

I have a boot sector that does the following, tested as a floppy in qemu-system-i386:

.code16
movw $0xd00, %bx                           ## machine code: 0xbb 0x00 0x0d
*Switch to Protected mode here, omitted*
.code32
movw $0xd00, %bx                           ## machine code: 0x66 0xbb 0x00 0x0d
*Switch back to real mode here, omitted*
.code16
movw $0xd00, %bx                           ## machine code: 0xbb 0x00 0x0d

This should work fine, but the processor gets confused in the last line, it doesn't move the value and starts jumping around. If I omit the last ".code16" (which adds a 0x66 to the machine code of the last line) then everything works fine. Why is that?

At first I thought that the procesor was not switching back to real mode, that would explain it, but it is kind of switching: after the last line cr0 holds 0x10, and I checked that segmentation works as for real mode.

Complete code:

.code16
  movw $0xd00, %bx   ## machine code: 0xbb 0x00 0x0d
  cli
  lgdt gdt_register
  movl %cr0, %eax
  orb $1, %al
  movl %eax, %cr0
  jmp $0x8, $protected

.code32
protected:
  sti
  movw $0xd20, %bx   ## machine code: 0x66 0xbb 0x00 0x0d        
back_to_realmode:
  cli
  lidtl idt_48
  movl %cr0, %eax
  andb $0xfe, %al
  movl %eax, %cr0
  jmp $0x0, $real

.code16
real:
  sti
  movw $0xd00, %bx
## gdt, idt, magic number, omitted..

Upvotes: 4

Views: 66

Answers (0)

Related Questions