Linux systemcall x86 and x64

Bare metal programming in PureBasic, for experienced users
infratec
Always Here
Always Here
Posts: 6817
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Linux systemcall x86 and x64

Post by infratec »

Hi,

big problem with small code.

This works on x86:

Code: Select all

Procedure.i inotify_init()
    !mov  eax, 291
    !xor  ebx, ebx
    !xor  ecx, ecx
    !xor  edx, edx
    !int  byte  0x80
    !test eax, eax
    !js   _inotify_init_error                            ;Jump short if sign (SF=1) (if <> 0 => error)
    ProcedureReturn                               ; return value in EAX
    !_inotify_init_error:
    !mov  [v_inotify_errno], eax
    ProcedureReturn -1
EndProcedure
But this works not on x64:

Code: Select all

Procedure.i inotify_init()
    !mov  rax, 253
    !xor  rbx, rbx
    !xor  rcx, rcx
    !xor  rdx, rdx
    !int  byte  0x80
    !test rax, rax
    !js   _inotify_init_error                            ;Jump short if sign (SF=1) (if <> 0 => error)
    ProcedureReturn                               ; return value in RAX
    !_inotify_init_error:
    !mov  [v_inotify_errno], rax
    ProcedureReturn -1
EndProcedure 
On x64 it returns -1

The diifferent values for the same function (291 and 253) should be correct.

Any ideas?

Bernd

P.S.: I don't want to install libnotify, because this installs many things of X and I have only a small console only system.
infratec
Always Here
Always Here
Posts: 6817
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Linux systemcall x86 and x64

Post by infratec »

Ok,

first bug found:

in x64 you have to use

Code: Select all

!syscall
instead of

Code: Select all

!int byte 0x80
inotify_init() works now.
But now I stuck at inotify_add_watch() :cry:

Bernd
infratec
Always Here
Always Here
Posts: 6817
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Linux systemcall x86 and x64

Post by infratec »

Ok,

works now.

Also the registers are different:

http://man7.org/linux/man-pages/man2/syscall.2.html

Code: Select all

  Procedure.i inotify_init()
    !mov  rax, 253
    !xor  rdi, rdi
    !xor  rsi, rsi
    !xor  rdx, rdx
    !syscall
    !test rax, rax
    !js   _inotify_init_error     ;Jump short if sign (SF=1) (if <> 0 => error)
    ProcedureReturn               ; return value in RAX
    !_inotify_init_error:
    !mov  [v_inotify_errno], rax
    ProcedureReturn -1
  EndProcedure 
Hope this helps someone to save many hours.

Bernd
User avatar
idle
Always Here
Always Here
Posts: 5040
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: Linux systemcall x86 and x64

Post by idle »

here's a link to a linux x64 sys call table
http://blog.rchapman.org/post/368010388 ... for-x86-64

Code: Select all

Global inotify_errno

Procedure.i inotify_init()
    !mov  rax, 253
    !syscall
    !test rax, rax
    !js   _inotify_init_error     ;Jump short if sign (SF=1) (if <> 0 => error)
    ProcedureReturn               ; return value in RAX
    !_inotify_init_error:
    !mov  [v_inotify_errno], rax
    ProcedureReturn -1
  EndProcedure 
  
Procedure.i inotify_add_watch(fd.l,charpath.s,mask.i)    ;rdi rsi rdx r10 r8 r9
  !mov rax, 254 
  !mov rdi, [p.v_fd]
  !mov rsi, [p.v_charpath]
  !mov rdx, [p.v_mask]
  !syscall
  !test rax, rax
  !js   _inotify_add_error     ;Jump short if sign (SF=1) (if <> 0 => error)
    ProcedureReturn               ; return value in RAX
  !_inotify_add_error:
  !mov  [v_inotify_errno], rax
    ProcedureReturn -1
EndProcedure     

Procedure inotify_rm_watch(fd.l,wd.i)
  !mov rax, 255 
  !mov rdi, [p.v_fd]
  !mov rsi, [p.v_wd]
  !syscall
  !test rax, rax
  !js   _inotify_rm_error     ;Jump short if sign (SF=1) (if <> 0 => error)
    ProcedureReturn               ; return value in RAX
  !_inotify_rm_error:
  !mov  [v_inotify_errno], rax
    ProcedureReturn -1
EndProcedure     

Procedure sys_read(fd,*buffer,size)
  !mov rax, 0
  !mov rdi, [p.v_fd]
  !mov rsi, [p.p_buffer]
  !mov rdx, [p.v_size]
  !syscall
  !test rax, rax
  !js   _sys_read_error     ;Jump short if sign (SF=1) (if <> 0 => error)
    ProcedureReturn               ; return value in RAX
  !_sys_read_error:
  !mov  [v_inotify_errno], rax
    ProcedureReturn -1
EndProcedure     
Windows 11, Manjaro, Raspberry Pi OS
Image
Post Reply