flat assembler
Message board for the users of flat assembler.

Index > Linux > convert win64 api calls to linux syscalls

Author
Thread Post new topic Reply to topic
tthsqe



Joined: 20 May 2009
Posts: 767
tthsqe 06 Jan 2014, 23:59
Could anybody who is familiar with linux try to convert some of these api calls in windows to syscalls in linux?

Code:
;;;;;;;;;;;;;;;;;;; windowz specific code, only uses kernerl32.dll ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; calling convention is 64 bit windows, except _ReadIn, _WriteOut, and _writeErr use rsi/rdi


_CreateThread:  ; rcx is start address
                ; rdx is parameter to pass
                        sub  rsp,8*9
                        mov  r8,rcx
                        mov  r9,rdx
                        xor  ecx,ecx
                        xor  edx,edx
                        mov  qword[rsp+8*4],rcx
                        mov  qword[rsp+8*5],rcx
                       call  qword[CreateThread]
                        add  rsp,8*9
                        ret


_ExitProcess:   ; rcx is exit code
                        sub  rsp,8*5
                       call  qword[ExitProcess]
                       int3


_ExitThread:   ; rcx is exit code
                        sub  rsp,8*5
                       call  qword[ExitThread]
                       int3


_GetStdHandles: ; no arguments
                        sub  rsp,8*5
                        mov  ecx,STD_INPUT_HANDLE
                       call  [GetStdHandle]
                        mov  qword[StdIn],rax
                        mov  ecx,STD_OUTPUT_HANDLE
                       call  [GetStdHandle]
                        mov  qword[StdOut],rax
                        mov  ecx,STD_ERROR_HANDLE
                       call  [GetStdHandle]
                        mov  qword[StdError],rax
                        add  rsp,8*5
                        ret


_QueryPerformanceCounter:  ; rcx is address
                        sub  rsp,8*5
                       call  qword[QueryPerformanceCounter]
                        add  rsp,8*5
                        ret


_QueryPerformanceFrequency:  ; no arguments
                        sub  rsp,8*5
                        lea  rcx,[Frequency]
                       call  qword[QueryPerformanceFrequency]
                        add  rsp,8*5
                        ret


_ReadIn:        ; rsi points to string to write stdin
                        sub  rsp,8*7
                        xor  eax,eax
                        mov  rcx,qword[StdIn]
                        mov  rdx,rsi
                        mov  r8d,1024
                        lea  r9,[temp]
                        mov  qword[rsp+8*4],rax
                       call  qword[ReadFile]
                        add  rsp,8*7
                        ret


_VirtualAlloc:  ; rcx is size
                        sub  rsp,8*5
                        mov  rdx,rcx
                        xor  ecx,ecx
                        mov  r8d,MEM_COMMIT
                        mov  r9d,PAGE_READWRITE
                       call  qword[VirtualAlloc]
                        add  rsp,8*5
                        ret


_VirtualFree:   ; rcx is address
                        sub  rsp,8*5
                        xor  edx,edx
                        mov  r8d,MEM_RELEASE
                       call  qword[VirtualFree]
                        add  rsp,8*5
                        ret

_WaitForMultipleObjects:   ; rcx is number of objects
                           ; rdx is address of handle array
                        sub  rsp,8*5
                        mov  r8d,1
                         or  r9,-1
                       call  qword[WaitForMultipleObjects]
                        add  rsp,8*5
                        ret




_WriteError:    ; rdi points to null terminated string to write to stderr
                        sub  rsp,8*7
                        mov  rdx,rdi
                         or  ecx,-1
                        xor  eax,eax
                repne scasb
                        not  ecx
                        lea  r8,[rcx-1]
                        mov  rcx,qword[StdError]
                        lea  r9,[temp]
                        mov  qword[rsp+8*4],rax
                       call  qword[WriteFile]
                        add  rsp,8*7
                        ret


_WriteOut:      ; rdi points to null terminated string to write to stdout
                        sub  rsp,8*7
                        mov  rdx,rdi
                         or  ecx,-1
                        xor  eax,eax
                repne scasb
                        not  ecx
                        lea  r8,[rcx-1]
                        mov  rcx,qword[StdOut]
                        lea  r9,[temp]
                        mov  qword[rsp+8*4],rax
                       call  qword[WriteFile]
                        add  rsp,8*7
                        ret    
Post 06 Jan 2014, 23:59
View user's profile Send private message Reply with quote
typedef



Joined: 25 Jul 2010
Posts: 2909
Location: 0x77760000
typedef 07 Jan 2014, 02:23
just use POSIX functions.
Post 07 Jan 2014, 02:23
View user's profile Send private message Reply with quote
HaHaAnonymous



Joined: 02 Dec 2012
Posts: 1178
Location: Unknown
HaHaAnonymous 07 Jan 2014, 03:53
[ Post removed by author. ]


Last edited by HaHaAnonymous on 28 Feb 2015, 18:35; edited 1 time in total
Post 07 Jan 2014, 03:53
View user's profile Send private message Reply with quote
randall



Joined: 03 Dec 2011
Posts: 155
Location: Poland
randall 07 Jan 2014, 11:00
This is simple code framework which shows how to implement 'CreateThread', 'WaitForMultipleObjects' and 'VirtualAlloc' on Linux 64.


Description:
Download
Filename: fractal1.asm
Filesize: 6.92 KB
Downloaded: 602 Time(s)

Post 07 Jan 2014, 11:00
View user's profile Send private message Visit poster's website Reply with quote
tthsqe



Joined: 20 May 2009
Posts: 767
tthsqe 07 Jan 2014, 14:36
OK, thanks! I see that you wait for each thread in sequence in place of one call to waitformultipleobjects. Also, I see that you didn't pass any arguments to the thread but do use xadd [#],1 to get the tile number. Is there a directly way of passing parameters to threads (i.e. in registers)?
Post 07 Jan 2014, 14:36
View user's profile Send private message Reply with quote
gens



Joined: 18 Feb 2013
Posts: 161
gens 07 Jan 2014, 19:18
afaik no
linux has only basic threading in kernel so you need to use clone(like_randall_does) to get a "full" thread

bdw POSIX threads in glibc are done using clone() + http://www.akkadia.org/drepper/futex.pdf
again, like randall does
Post 07 Jan 2014, 19:18
View user's profile Send private message Reply with quote
randall



Joined: 03 Dec 2011
Posts: 155
Location: Poland
randall 08 Jan 2014, 07:29
Yes, it is possible to pass parameters to the threads via registers (code from http://board.flatassembler.net/topic.php?t=14227):

Code:
; rsi ptr to function, rdi arg to function , rdx top of stack
; returns pid in eax
start_thread:
        push rbp
        mov rbp,rsp
        mov rsp, rdx;
        push rdi rsi
        sub rdx,16    ; adjust stack for thread
        mov rsp,rbp
        
        sys_clone rdx
        test eax,eax
        jnz .L0
        ; child
        pop rsi rdi
        call rsi
        sys_exit 0
.L0:   ; parent
        pop rbp
        ret
    
Post 08 Jan 2014, 07:29
View user's profile Send private message Visit poster's website Reply with quote
gens



Joined: 18 Feb 2013
Posts: 161
gens 08 Jan 2014, 09:51
oh
ye that was dumb of me since the whole program gets copied so do the registers

rbx, rbp, rsp and r12-15 survive syscalls (and can be used)
http://www.logix.cz/michal/devel/amd64-regs/

i will need to test all this thou
Post 08 Jan 2014, 09:51
View user's profile Send private message Reply with quote
randall



Joined: 03 Dec 2011
Posts: 155
Location: Poland
randall 08 Jan 2014, 10:33
gens wrote:
oh
ye that was dumb of me since the whole program gets copied so do the registers

rbx, rbp, rsp and r12-15 survive syscalls (and can be used)
http://www.logix.cz/michal/devel/amd64-regs/

i will need to test all this thou


Yes, in my code I use rbx and it works.
Post 08 Jan 2014, 10:33
View user's profile Send private message Visit poster's website Reply with quote
tthsqe



Joined: 20 May 2009
Posts: 767
tthsqe 08 Jan 2014, 17:44
great, I will start compiling a list of equivalent functions and see if they work. Fell free to comment. Should there be an analogue of GetStdHandles? or are these always 0,1, and 2?
Code:
_ExitProcess:   ; rcx is exit code
        mov  eax,60
        mov  edi,ecx
    syscall

_VirtualAlloc:   ; rcx is size
       push  rsi rdi
        mov  eax,9                            ; sys_mmap
        mov  rsi,rcx                          ; length
        xor  edi,edi                          ; addr
        mov  edx,0x1+0x2                      ; PROT_READ | PROT_WRITE
        mov  r10d,0x02+0x20                   ; MAP_PRIVATE | MAP_ANONYMOUS
         or  r8,-1                            ; fd
        xor  r9d,r9d                          ; offset
    syscall
        pop  rdi rsi
        ret   

    
Post 08 Jan 2014, 17:44
View user's profile Send private message Reply with quote
randall



Joined: 03 Dec 2011
Posts: 155
Location: Poland
randall 09 Jan 2014, 07:28
Always 0, 1, 2.
Post 09 Jan 2014, 07:28
View user's profile Send private message Visit poster's website Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.