1

I am working on an OS using EFI using the ms_abi and was wondering how does it pass arguments because I am moving some systemtable info into a register but when I try to call a function using the correct offset it freezes. I think this is because of a incorrect use of the ABI.

I tried using what is on Wikipedia but I don't think it was working. It could be that I am not using the ABI correctly. I tried using RCX and RDX but those are not working. I also looked at the GCC documentation but that was no help either.

This is what i am trying

    .intel_syntax noprefix
    .global efimain 

efimain:
    mov [systable],rcx 
    mov rax,[systable + 40h] 
    mov rax, [rax+18h] 
    mov  rdx,Msg 
    call rax
    mov rax, 0 
    ret

systable: 
    .byte 122 
Msg:
    .word 'H'
1
  • I assume from the code you are trying to use OutputString on the Simple Text Output Interface? Commented May 30, 2024 at 21:15

2 Answers 2

4

Several things wrong here.

  1. The MS ABI requires 20h bytes of unused space at the top of the stack before the call. (In the called function, this space is just above the return address.)

  2. The stack must be aligned to a multiple of 10h before the call.

  3. The system table is the second parameter to efimain. It is a pointer. To access a field in the system table, you have to use the pointer.

  4. The location where you store the system table pointer must be a qword, not a byte.

  5. The address of the protocol must be passed as the first parameter to OutputString.

  6. The offset of OutputString is 8.

  7. The address of Msg should be loaded using RIP-relative addressing.

  8. The string passed to OutputString must be null-terminated.

         .intel_syntax noprefix
         .global efimain 
    
     efimain:
         sub rsp, 28h
         mov [rip+systable],rdx 
         mov rcx,[rdx+40h]
         lea rdx,[rip+Msg]
         call [rcx+08h]
         mov rax, 0
         add rsp, 28h
         ret
    
     systable: 
         .quad 0
     Msg:
         .word 'H',10,0
    
Sign up to request clarification or add additional context in comments.

3 Comments

Just to add, ms docs their x64 calling convention here.
It did not work
See section 2.3.4 of the UEFI Specification for the X64 platform callling convention.
0

I consulted section 2.3.4 of the UEFI Specification for the X64 platform calling convention as suggested by fpmurphy in the comments. It turned out I was wrong. rdx is the systemtable and rcx is the ImageHandle.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.