Page 1 of 1

QEMU fails to execute lea 0x1(%rax),%rax

Posted: Tue Dec 29, 2020 12:08 am
by fbkr
Hi all,

I'm attempting to port my OS to x86_64, but it has been a horrible experience so far. Currently, I'm stuck at getting strlen to work.

First, let me paste this:

Code: Select all

(gdb) disas
Dump of assembler code for function strlen:
   0x00000000000012eb <+0>:     lea    -0x1(%rdi),%rax
   0x00000000000012ef <+4>:     cmpb   $0x0,0x1(%rax)
=> 0x00000000000012f3 <+8>:     lea    0x1(%rax),%rax
   0x00000000000012f7 <+12>:    jne    0x12ef <strlen+4>
   0x00000000000012f9 <+14>:    sub    %rdi,%rax
   0x00000000000012fc <+17>:    retq
End of assembler dump.
(gdb) i r rax
rax            0x1fff              8191
(gdb) si
0x00000000000012f4      95        while (*str)
(gdb) si
0x00000000000012f7      95        while (*str)
(gdb) i r rax
rax            0x1fff              8191
Here, qemu is at the lea and once it executes, I expect rax to become 0x2000, however, as you can see, it just stays `0x1fff` and keeps looping forever. Also, it steps to 0x12f4, which doesn't exist in the disassembly.

This is how I run qemu:

Code: Select all

qemu-system-x86_64-m 256m -cpu qemu64 -serial mon:stdio -kernel .\bin\i386-loader -s
i386-loader just sets up paging and jumps to a 64-bit function that attempts to write something to the screen, but it just gets stuck in this strlen.

I've been fighting this for a few days now and wanted to reach out to see if anyone has any idea...

Re: QEMU fails to execute lea 0x1(%rax),%rax

Posted: Tue Dec 29, 2020 12:17 am
by Octocontrabass
fbkr wrote:i386-loader just sets up paging and jumps to a 64-bit function
Is the CPU in 64-bit mode when it jumps to said function?

Re: QEMU fails to execute lea 0x1(%rax),%rax

Posted: Tue Dec 29, 2020 12:23 am
by fbkr
Oh sorry, yes, it also enables long mode.

At the point of strlen, I also have these:

Code: Select all

(gdb) i r efer
efer           0x500               [ LMA LME ]
(gdb) i r cr0
cr0            0x80000011          [ PG ET PE ]
(gdb) i r cr3
cr3            0x7000              [ PDBR=0 PCID=0 ]
(gdb) i r cr4
cr4            0x20                [ PAE ]

Re: QEMU fails to execute lea 0x1(%rax),%rax

Posted: Tue Dec 29, 2020 12:26 am
by Octocontrabass
What about CS?

Re: QEMU fails to execute lea 0x1(%rax),%rax

Posted: Tue Dec 29, 2020 1:09 am
by fbkr
Ugh, you're right. I missed that a far jump would be required from 32->64 bit, I was having a regular jump. Converting it to a far jump fixed it. Thank you!