January 23, 2008

execve() starts to take shape

Despite the lack of a memory allocator and a working file system, I decided to start working on the execve() system call. In its current state, it doesn't load the text and data sections from a file; instead, it uses a hard-coded location for the program code, and it doesn't have a data section. Also, the stack pointer points to a global array variable with a fixed size. However, execve() has rudimentary code to pass arguments and environment variables to the new process image through its stack.

As a result, instead of executing /etc/init as it expects, the kernel's icode() actually executes something entirely different. I took the opportunity to test the argument- and environment-passing mechanism of execve() inside the new user program simply by printing the arguments and environment passed to it. That program essentially consists of this code:
#include <stdio.h>
#include <string.h>

static void println(char *s)
{
write(2, s, strlen(s));
write(2, "\n", 1);
}

int main(int argc, char *argv[], char *envp[])
{
println("This is a user program.");

println("\narguments:");
for (; *argv; ++argv)
println(*argv);

println("\nenvironment:");
for (; *envp; ++envp)
println(*envp);

return 0;
}
which is a fully-working standalone C program. You can run the above program and see all of the arguments and environment passed to it. Here is the result of running this program in Punix (the text after the copyright and warranty information but before the kernel panic message):



The arguments and environment are those passed to it by icode(). The first argument to the program, "-00000", comes from 2.11BSD (one of many sources I'm using to write Punix). I kept it in there when I transliterated my icode() routine from PDP to 68k assembly language code. When I get a real "init" program I'll change the argument to whatever it expects and accepts as arguments (such as to specify the run level to enter after booting). It will also depend on the (possible) presence of a boot loader that can pass arguments to the kernel and to init. That will be some time in the future, so I won't worry about it for now.

No comments: