Skip to content

49k · exec

Released Tue Dec 1, in class: posted here early

This is the README OSlings will give every student for 49k_exec (oslings lesson shows it) when the exercise is released on Tue Dec 1. The starter code is not published here: it reaches you through OSlings, in class on the class network. Back to All Exercises.

Learn -> Understand -> Implement. Exercise 48k ran one program, hard-coded onto one page. That is not how an OS works: an OS keeps a whole collection of programs and starts whichever one you name, with whatever arguments you type. This exercise builds that. A given loader copies a program of any size into a fresh address space; you write the exec recipe that loads the program, gives it a stack and its command-line arguments, and points the CPU at it - the way exec does on every Unix-like system.

Learn

What exec is

On Unix, starting a program is two steps: fork makes a copy of the current process (that is exercise 51k), and exec throws away that copy's memory and loads a new program in its place. This exercise is the exec half: given a program and some arguments, build the address space that program needs and point the CPU at its first instruction.

Three things make it more than exercise 48k's one-page loader:

  1. Programs live in a table, looked up by name. run echo finds the program called echo. We keep a small table of programs (hello, args, echo, big), each a flat binary baked into the kernel image.
  2. A program can be bigger than one page. Exercise 48k copied one page and was done. A real loader copies however many pages the program needs. That is load_segment, which is given - you will read it.
  3. A program receives arguments. run echo hello world has to get the words hello and world into the program somehow. That is argv, and setting it up is the meat of exec.

A flat binary, and where it goes

We do not have a compiler for user programs yet (that is exercise 53k). So each program is a short, hand-written, position-independent chunk of machine code, stored in the kernel image as plain data between two labels (prog_echo_start .. prog_echo_end). A flat binary just means "the raw instruction bytes, with no header" - as opposed to a real executable file format like ELF, which wraps the code in metadata describing where each part should load. Flat binaries are the simplest thing that works, and ours are small enough to always load at virtual address 0.

Position-independent means the code does not care what address it runs at: it never hard-codes an address, only computes them relative to the current program counter (the pc, the register holding the address of the instruction being executed). That matters because we copy the program to virtual address 0 in the user's world, which is a different address than where it sits in the kernel image. The la (load address) instruction with .option norelax produces pc-relative address math, so the program works wherever it lands. You do not have to write any of this - the programs are given - but it is worth knowing why they look the way they do.

load_segment: the loader (given)

load_segment (vm.rs) is the general form of exercise 48k's map_user_pages. The idea is identical - allocate a physical page, copy code onto it, map it into the user's page table with the user bit (PTE_U) - but now in a loop, one PGSIZE chunk of the image at a time, until the whole image is copied:

image bytes:  [======== 5000 bytes ========]
                    |                |
              page 0 (4096)     page 1 (904, rest zero-filled)
                    |                |
   mapped at VA 0x0000        mapped at VA 0x1000     (both R + X + U)

Read the given load_segment beside map_user_stack, right below it: the stack is the one-page version of the same recipe, minus the copy. The big entry in the program table is padded past 4096 bytes, so loading it makes the loop run twice; the harness checks that both pages end up mapped.

argv: how a program receives its arguments

When you type run echo hello world, the program needs two things:

  • argc = the argument count (here 3: the program name echo, plus hello and world).
  • argv = a pointer to an array of pointers, one per argument, each pointing at a NUL-terminated string. (A NUL terminator is a single zero byte marking the end of a string - that is how C-style strings know where they stop, since they carry no length.)

By our calling convention, a program finds these in two registers on entry: a0 = argc and a1 = argv. (This mirrors C's int main(int argc, char **argv).)

The tricky part is building argv. The strings and the pointer array have to live in the user's memory (the program cannot read the kernel's), so we lay them out on the user's stack, from the top downward:

   high addresses
      "echo\0"  "hello\0"  "world\0"      <- the argument strings
      argv[0]  argv[1]  argv[2]  NULL      <- pointers to those strings, + a NULL
   low addresses   <- the stack pointer ends here; this address IS argv

Every pointer stored in the argv array is a user virtual address - where the string sits in the program's address space, not the kernel's. We write it all into the user's memory with copyout, the function given in exercise 48k (the mirror of copyin) that sat unused until now. Read push_argv closely: it is given, and it is the clearest picture you will get of what argv really is.

Putting it together: build_process

exec allocates a process, then calls build_process (your piece) to fill it in. build_process is a recipe with six steps, and every step is a function that already exists:

  1. look the program up by name,
  2. map the trampoline + trapframe (exercise 48k's proc_pagetable),
  3. load the image (load_segment, given),
  4. give it a stack (map_user_stack, given),
  5. push the arguments (push_argv, given), which hands back (argc, argv, sp),
  6. set the trapframe so the program starts at its first instruction, on that stack, with a0 = argc and a1 = argv.

Each fallible step returns a Result, so you chain them with the ? operator (exercise 40k). If any step fails, exec frees the half-built process for you.

Understand

Read these, in order:

  1. rv6/src/exec.rs, in four parts:
    • the calling-convention note at the top of the programs block (a0 = argc, a1 = argv), then only the one-line # ---- name: ... ---- header above each of the four programs - skip the assembly itself;
    • Program, programs, and lookup: the table and the by-name search;
    • exec (the cleanup wrapper) and build_process, the one you will write;
    • push_argv - read this one slowly; it is the argv layout made real.
  2. rv6/src/vm.rs: load_segment (given) with map_user_stack right below it as the one-page model, then copyout, which push_argv now uses.
  3. rv6/src/memlayout.rs: USER_CODE, MAX_PROG_PAGES, USER_STACK, and USER_STACK_TOP - the stack now sits at a fixed address above the largest possible image, and the unmapped gap between them catches a program that runs off its end.
  4. rv6/src/main.rs: skim exec_self_check - the harness runs args (checks argc), echo (checks the argument strings arrive), and big (checks a two-page image loads), then makes sure an unknown name fails cleanly.

Control flow of run echo hello world:

shell: exec("echo", ["hello","world"])
   -> allocproc                          a blank process
   -> build_process:                                            <- YOU
        lookup("echo")                   find the program
        proc_pagetable                   map trampoline+trapframe
        load_segment(image)              copy the program in (given)
        map_user_stack                   give it a stack
        push_argv(name,args)             lay out argv -> (argc,argv,sp)
        trapframe: epc=0, sp, a0=argc, a1=argv
   -> run(p)                             drop to user mode (exercise 48k)
        echo reads a0/a1, writes each argument, exit(0)
   -> "hello world"

Implement

One piece:

exec.rs - build_process: the six-step recipe above, using ? on each step. Steps 1-5 are calls to functions that already exist; step 6 fills in four trapframe fields. The // IMPLEMENT comment gives the exact call for every line.

Check your work:

oslings run 49k_exec
# or
oslings watch

The harness checks each idea in turn: arguments reach the program, the argument strings arrive intact, and a two-page program loads.

Then the payoff:

cd rv6 && cargo run        # boots to the rv6$ prompt

Try progs to list the programs, then:

run echo hello world
run args a b c             (exits with status = argc = 4)
run hello
run big

You are now typing a command, and the kernel is finding that program, building it a private world, handing it your words, and running it - the same loop a real shell runs. (Exit QEMU with Ctrl-A then X.)

Stuck? oslings hint.