hey,
i would like to know how to write a system library, how to enable fork() and POSIX functions in the library, etc.
system library how-to
system library how-to
oh microsoft, microsoft, what souls you have dismayed
Hi,
First off, you will need kernel support functions.
Take fork() (which I believe copies the current task space but I've never programmed on linux). You will need some kind of function in your system call library which forks the current process. The actual user-space library then just calls this function. For example, in user-space:
These functions are obviously not in the same source file. This assumes your kernel provides system services on int 0x30, for whatever reason.
Now, your kernel needs to handle int 0x30 with some kind of function lookup table. As you have called it with eax = 0x01, it will jump to handled function 1:
That's about the size of it for all system services you plan on providing. For an idea about what services OS's generally provide, have a look here, where you can see what services DOS and Windows provide and here for linux system calls.
HTH
Adam
First off, you will need kernel support functions.
Take fork() (which I believe copies the current task space but I've never programmed on linux). You will need some kind of function in your system call library which forks the current process. The actual user-space library then just calls this function. For example, in user-space:
Code: Select all
void fork()
{
syscall(0x01);
}
syscall:
pop eax
int 0x30
ret
Now, your kernel needs to handle int 0x30 with some kind of function lookup table. As you have called it with eax = 0x01, it will jump to handled function 1:
Code: Select all
int fn0x01()
{
clonecurrentprocess();
return(successvalue);
}
HTH
Adam
i actually...
I actually created an interrupt handler for fork() the system defines fork() pid_t as an interrupt at 0x80 then I wrote the interrupt handler for it when i use fork(), so the system library associates it with 0x80
oh microsoft, microsoft, what souls you have dismayed
Good - hope it all works. Just a couple of (small) points.
1. As you are likely to want several system calls, it is fairly usual to use EAX as a function selector. This means that INT 0x80 with EAX 0x02 may be fork, whereas INT 0x80 with EAX 0x01 may be exit process (as in Linux).
2. I have no idea of the ultimate goal of your OS, but if you ever plan on emulating Linux system calls, Linux uses int 0x80, so you may want to steer clean of that number. If you ever plan on other people writing code for your OS, it may be confusing for people porting Linux code that your int 0x80 function 0x10 is "Load Driver", whereas the Linux version is "unlink" (for the sake of argument).
Cheers,
Adam
1. As you are likely to want several system calls, it is fairly usual to use EAX as a function selector. This means that INT 0x80 with EAX 0x02 may be fork, whereas INT 0x80 with EAX 0x01 may be exit process (as in Linux).
2. I have no idea of the ultimate goal of your OS, but if you ever plan on emulating Linux system calls, Linux uses int 0x80, so you may want to steer clean of that number. If you ever plan on other people writing code for your OS, it may be confusing for people porting Linux code that your int 0x80 function 0x10 is "Load Driver", whereas the Linux version is "unlink" (for the sake of argument).
Cheers,
Adam
or write yourself a tool/script that generates those call stubs automatically from a list of functions with the number of parameters. That script could then also automatically generate the system call table for the kernel. Using headers and a static library (or just compile it into the binary) would then enable you to simply do a fork(), or any other system call without having to think about system call numbers.
The downside is that all libraries/applications that use that static library need to be in sync with the kernel. That means that you'd have to re-compile all those components every time you modify your system call list. To minimize that, you can add another layer between applications and the kernel. That means that you have a native library that all other libraries and programs link against dynamically, so that only the native library does system calls directly. This way applications and libraries are still working even if you change or add/remove system calls. This is how windows does it, btw.
The downside is that all libraries/applications that use that static library need to be in sync with the kernel. That means that you'd have to re-compile all those components every time you modify your system call list. To minimize that, you can add another layer between applications and the kernel. That means that you have a native library that all other libraries and programs link against dynamically, so that only the native library does system calls directly. This way applications and libraries are still working even if you change or add/remove system calls. This is how windows does it, btw.
Yes, try and keep all your syscalls using the same interrupt, then you can change to using sysenter/sysexit without too many problems. To make it even easier to switch at will, define a SYSCALL macro in you libc and use that instead, e.g.AJ wrote:1. As you are likely to want several system calls, it is fairly usual to use EAX as a function selector. This means that INT 0x80 with EAX 0x02 may be fork, whereas INT 0x80 with EAX 0x01 may be exit process (as in Linux).
Code: Select all
#define SYSCALL(func, arg, ret) __asm volatile ( "int $0x80" : "=a" (ret) : "a" (func), "b" (arg) );
Code: Select all
#define SYSCALL(func, arg, ret) __asm volatile ( "sysenter" : "=a" (ret) : "a" (func), "b" (arg) );
I suggest you list your function numbers as defines in a header file that is included both into your kernel and libc, so that you can easily keep them in sync.
Regards,
John.
what
do i need to add anything to my IDT if i create a new interrupt? a new isr?
i have currently 31 ISR for the Intel exception messages and such, but do i need to reconfigure a new ISR for my fork() and system calls ints?
i have currently 31 ISR for the Intel exception messages and such, but do i need to reconfigure a new ISR for my fork() and system calls ints?
oh microsoft, microsoft, what souls you have dismayed