Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: System Call Curiosity



On woensdag, jun 25, 2003, at 09:53 Europe/Brussels, Sam Hart wrote:

The code below compiles and runs without problems on OpenBSD and Linux.

Nevertheless, it's wrong.

---------- cut here -------------
.data
msg:
.string "Hello world!\n"
len = . - msg
.text
.global _start
_start:
li 0,4
li 3,1
lis 4,msg@ha
addi 4,4,msg@l
li 5,len
sc

Most (all?) OS'es on the PowerPC transfer control to a different instruction after you executed an "sc" depending on whether or not an error occurrend. For Linux, this is the instruction right after the "sc" if there was no error and the instruction after that if an error did occur. There's even an extra condition on Linux: if the syscall returns to the instruction right after it and the sign flag is set, there still is an error. So you have to add here:

bns Lno_error1
neg r3,r3
bl your_error_handler
Lno_error1:

li 0,1
li 3,0
sc
---------- cut here -------------

With a little fiddling of the syntax I got converted it to Darwin

Note that under Darwin, using syscalls directly is not supported.

---------- cut here -------------
.data
.cstring
.align 2
msg:
.asciz "Hello world!\n"
len = . - msg
.text
.align 2
.globl _start
_start:
li r0,4
li r3,1
lis r4,ha16(msg)
ori r4,r4,lo16(msg)
li r5,len
sc

Darwin does it the other way round compared to Linux (from Libc/ppc/sys/SYS.h):

#define SYSCALL(name, nargs) \
.globl cerror @\
LEAF(_##name) @\
kernel_trap_args_##nargs @\
li r0,SYS_##name @\
sc @\
b 1f @\
b 2f @\
1: BRANCH_EXTERN(cerror) @\
.text \
2: nop

So it return to the instruction after the sc if an error occurred and the instruction after that if no error occurred.


Jonas
_______________________________________________
darwin-development mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/darwin-development
Do not post admin requests to the list. They will be ignored.

References: 
 >System Call Curiosity (From: Sam Hart <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.