Page 1 of 1

Newlib syscalls

Posted: Sun Aug 05, 2012 3:21 am
by staalmannen
Hi

I came to here after I read the wiki entry on "porting newlib" [1] and I have been searching these forums and the rest of the web without much succes (which I find strange!). Perhaps I am just plain stupid ;)

Background

I do porting of stuff to the Plan9 operating system [0] as a hobby project [2] and one thing I am now trying to do is to update an old GCC-based toolchain that was ported back in 2002 (binaries of the original gcc port can be found at [3] or if Bell labs is down a mirror at [4]). The status of updating the GCC stuff can be read on the wiki [5]. I have updated the old APE (ansi- posix environment) libc from the old (3rd edition?) variant to a recent one and it can compile stuff. I am not entirely sure however if the new APE libc port to GCC is entirely safe and functional since I notice some crashes in some binaries compiled with it. I am thus considering whether I should switch the default libc for GCC to newlib since this is a better tested libc and a Plan9 port of it could also make it easier to make a cross-compiler for the i386-Plan9 target on other hosts.

Problem

- In the "porting newlib" wiki, 17 syscalls are mentioned that they need to be implemented in newlib for a port. It does however not mention where those syscalls should be implemented.
I have been browsing through the source and I did not find anything (yet again, it could simply be because I am stupid).

The Plan9 (i386) kernel intercepts syscalls at exception vector 40 (0x64) [6] and these are the syscalls:

Code: Select all

cat /sys/src/libc/9syscall/sys.h
#define	SYSR1		0
#define	_ERRSTR		1
#define	BIND		2
#define	CHDIR		3
#define	CLOSE		4
#define	DUP		5
#define	ALARM		6
#define	EXEC		7
#define	EXITS		8
#define	_FSESSION	9
#define	FAUTH		10
#define	_FSTAT		11
#define	SEGBRK		12
#define	_MOUNT		13
#define	OPEN		14
#define	_READ		15
#define	OSEEK		16
#define	SLEEP		17
#define	_STAT		18
#define	RFORK		19
#define	_WRITE		20
#define	PIPE		21
#define	CREATE		22
#define	FD2PATH		23
#define	BRK_		24
#define	REMOVE		25
#define	_WSTAT		26
#define	_FWSTAT		27
#define	NOTIFY		28
#define	NOTED		29
#define	SEGATTACH	30
#define	SEGDETACH	31
#define	SEGFREE		32
#define	SEGFLUSH	33
#define	RENDEZVOUS	34
#define	UNMOUNT		35
#define	_WAIT		36
#define	SEMACQUIRE	37
#define	SEMRELEASE	38
#define	SEEK		39
#define	FVERSION	40
#define	ERRSTR		41
#define	STAT		42
#define	FSTAT		43
#define	WSTAT		44
#define	FWSTAT		45
#define	MOUNT		46
#define	AWAIT		47
#define	PREAD		50
#define	PWRITE		51
If anyone has a pointer where I could find a more extensive documentation on how to implement the needed syscalls in newlib, that would be great!.
Since I already have a native (old) binutils/gcc (including Crt0), most of the stuff are basically in place already as soon as I get this "syscall glue" in place.

References
[0] http://en.wikipedia.org/wiki/Plan_9_from_Bell_Labs
[1] http://wiki.osdev.org/Porting_Newlib
[2] http://code.google.com/p/ports2plan9/
[3] http://plan9.bell-labs.com/sources/extra/gcc/gnubin.tgz
[4] http://code.google.com/p/ports2plan9/do ... gnubin.tgz
[5] http://code.google.com/p/ports2plan9/wiki/GNUonPlan9
[6] http://www.glendix.org/docs/glendix_report.pdf

Re: Newlib syscalls

Posted: Sun Aug 05, 2012 8:03 am
by bluemoon
The newlib's 17 (17 is minimum, and more you implement the more you get!) interface at user-space, at your implement you can invoke your kernel (by call, int, sysenter, syscall, UD2, etc), and optionally do some stuff before/after the kernel call; it's also legal to just do stuff without kernel at all (like return an error-feature-not-supported).
If anyone has a pointer where I could find a more extensive documentation on how to implement the needed syscalls in newlib, that would be great!.
Perhaps the official document?

Re: Newlib syscalls

Posted: Sun Aug 05, 2012 9:45 am
by staalmannen
bluemoon wrote:The newlib's 17 (17 is minimum, and more you implement the more you get!) interface at user-space, at your implement you can invoke your kernel (by call, int, sysenter, syscall, UD2, etc), and optionally do some stuff before/after the kernel call; it's also legal to just do stuff without kernel at all (like return an error-feature-not-supported).
If anyone has a pointer where I could find a more extensive documentation on how to implement the needed syscalls in newlib, that would be great!.
Perhaps the official document?
Very cool! so if I ever figure this out I should definitely aim to support as many of the syscalls as possible then...

About the official document: most info in there seems to be focused on a new hardware platform, and I could not really find info specifically for porting to a new OS.

I probably need the "newlib for dummies" version ;) Not quite at the level of you guys :P

Re: Newlib syscalls

Posted: Sun Aug 05, 2012 10:27 am
by bluemoon
I would suggest follow the osdev wiki for starter, once you have a minimal working environment you can implement extra functionality according to the API listing in the official document.

Re: Newlib syscalls

Posted: Sun Aug 05, 2012 11:01 am
by jnc100
If you have a working compiler for your target operating system then I suggest you try the 'Newlib' section of OS Specific Toolchain which has a detailed walkthrough of how to add an extra target to newlib and where to put the implementations of the syscalls. You could then submit your changes as a patch to the newlib maintainers.

Regards,
John.

Re: Newlib syscalls

Posted: Sun Aug 05, 2012 12:14 pm
by staalmannen
jnc100 wrote:If you have a working compiler for your target operating system then I suggest you try the 'Newlib' section of OS Specific Toolchain which has a detailed walkthrough of how to add an extra target to newlib and where to put the implementations of the syscalls. You could then submit your changes as a patch to the newlib maintainers.

Regards,
John.

Great! This looks like what I was looking for :) Now I have somewhere to start and just have to figure the rest out. Sending upstream would definitely be a good idea.