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

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
fbkr
Posts: 14
Joined: Sun Dec 13, 2020 4:06 pm

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

Post 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...
Octocontrabass
Member
Member
Posts: 5568
Joined: Mon Mar 25, 2013 7:01 pm

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

Post 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?
fbkr
Posts: 14
Joined: Sun Dec 13, 2020 4:06 pm

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

Post 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 ]
Octocontrabass
Member
Member
Posts: 5568
Joined: Mon Mar 25, 2013 7:01 pm

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

Post by Octocontrabass »

What about CS?
fbkr
Posts: 14
Joined: Sun Dec 13, 2020 4:06 pm

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

Post 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!
Post Reply