Show all solutions

Test 3

Notes

Assume a Linux operating system running on the x86-64 architecture unless otherwise stated. On this architecture, sizeof(int) == 4 and sizeof(long) == sizeof(void*) == 8.

Some figures and code samples can be pinned to your browser so you can keep them visible as you scroll through the test. Push the 📌 and then drag the pinned figure where you want it.

Some questions concern WeensyOS (and pset 3b). Unless a question says otherwise, assume a WeensyOS kernel that correctly completes all phases of the problem set, including fork, exit, and shared read-only memory. For reference:

Constant Value Meaning
PAGESIZE 0x1000 4096 bytes
PROC_START_ADDR 0x100000 First virtual address available to processes
MEMSIZE_PHYSICAL 0x200000 Size of physical memory (2 MiB, 512 pages)
MEMSIZE_VIRTUAL 0x300000 Process virtual addresses go up to here
CONSOLE_ADDR 0xB8000 The console page, user-accessible in every page table
KERNEL_START_ADDR 0x40000 Start of kernel code
MAXNPROC 16 Size of ptable; slot 0 is never used
PTE_P, PTE_W, PTE_U 1, 2, 4 Present, writable, user-accessible

The kernel always runs using kernel_pagetable, an identity mapping of physical memory (virtual address X maps to physical address X) in which no page is user-accessible except the console.

1. ARM that bomb (18 points)

The binary bomb runs on x86-64, but it could have been compiled for other processors. Here is one phase of a bomb—the same phase some of you defused—compiled for AArch64 (64-bit ARM), the architecture of Apple silicon and most phones. So it’s new syntax, but don’t worry—your experience with the bomb and x86-64 assembly will lead you to the right answers anyway!

phase_2:
        sub     sp, sp, 48
        str     x29, [sp]
        str     x30, [sp, 8]
        add     x1, sp, 24
        bl      read_six_numbers
        ldr     w2, [sp, 24]
        tbnz    w2, #31, .L3
        add     x3, sp, 28
        mov     w0, 1
.L4:
        add     w1, w2, w0
        ldr     w2, [x3]
        cmp     w2, w1
        bne     .L3
        add     w0, w0, 1
        add     x3, x3, 4
        cmp     w0, 6
        bne     .L4
        ldr     x29, [sp]
        ldr     x30, [sp, 8]
        add     sp, sp, 48
        ret
.L3:
        bl      explode_bomb

QUESTION 1A. x86-64’s stack pointer register is called %rsp. What is AArch64’s stack pointer register called?

QUESTION 1B. Write one or more x86-64 instructions that subtract 48 from %rsp.

QUESTION 1C. Write one or more AArch64 instructions that subtract 48 from AArch64’s stack pointer.

QUESTION 1D. What is the name of the AArch64 instruction that calls a function?

QUESTION 1E. Based on this assembly, can you identify any AArch64 register(s) that are callee-saved? Explain briefly.

QUESTION 1F. Based on this assembly, can you identify any AArch64 register(s) that are used for function arguments?

QUESTION 1G. x86-64 arithmetic instructions are written OP SRC, DST, which means DST := DST OP SRC. Describe in similar terms how AArch64 arithmetic instructions appear to be written.

QUESTION 1H. x86-64’s data movement instructions mostly use one mnemonic, mov (with b/w/l/q suffixes): depending on its arguments, mov can move data between registers or between a register and primary memory. What instruction mnemonic or mnemonics does AArch64 appear to use for data movement? Explain briefly.

QUESTION 1I. Which AArch64 instructions appear to be conditional branches? Explain briefly.

QUESTION 1J. The instruction on line 8 is called “Test Bit and Branch if Nonzero.” It takes three arguments: a register, the bit index to test, and the destination of the conditional branch. Write a C++ statement that could compile into this instruction. (You may assume that the six numbers read by read_six_numbers are stored in a variable called int numbers[6];.)

QUESTION 1K. What is a likely C++ type of the object corresponding to register x3?

QUESTION 1L. What is a likely C++ type of the object corresponding to register w0 on lines 10–19?

QUESTION 1M. (Hard!) Write a line that will pass this phase.

2. You can fork everything (13 points)

Elizabeth Holmes is working on WeensyOS. Her kernel is generally OK (kalloc, kfree, exit, process_setup with read-only code segments, etc. work as specified), but her fork is a fraud.

int fork() {
    pid_t pid = 1;
    while (pid <= MAXNPROC && ptable[pid].state != P_FREE) {
        ++pid;
    }
    if (pid > MAXNPROC) {
        return -1;
    }
    proc* child = &ptable[pid];
    child->pagetable = kalloc_pagetable();
    for (vmiter pit(current, 0), cit(child, 0);
         pit.va() < MEMSIZE_VIRTUAL;
         pit += PAGESIZE, cit += PAGESIZE) {
        if (!pit.user() || !pit.writable() || pit.va() == CONSOLE_ADDR) {
            cit.map(pit.pa(), pit.perm());
        } else {
            void* pg = kalloc(PAGESIZE);
            memcpy(pg, (void*) pit.va(), PAGESIZE);
            cit.map(pg, pit.perm());
        }
    }
    child->regs = current->regs;
    child->state = P_RUNNABLE;
    return pid;
}

Her kernel’s syscall function calls this as case SYSCALL_FORK: return fork();, having first copied the saved registers into current->regs.

QUESTION 2A. What is sys_fork() supposed to return?

QUESTION 2B. What does Elizabeth’s sys_fork() implementation return? Describe how to fix it.

QUESTION 2C. List the line numbers in Elizabeth’s fork that might fail in some way due to resource exhaustion, but where Elizabeth is not properly checking for failure.

For partial credit, explain in general terms what Elizabeth’s fork code should do when it experiences a resource exhaustion failure.

QUESTION 2D. When Elizabeth runs make run-fork, she observes the following kernel panic:

PANIC: Kernel page fault on 0x2ff000 (read missing page, rip=0x44f20)!
  #0  0x44f20  <memcpy>
  #1  0x413be  <_Z4forkv>
  #2  0x41b84  <_Z7syscallP8regstate>
  #3  0x40b28  <_Z13syscall_entryv>

This traces back to the memcpy on line 18. What’s wrong with that line, and how could she fix it?

QUESTION 2E. Elizabeth fixes those bugs and eventually moves on to Phase 7 and make run-exit. Some assertions trigger; she immediately comments out (tsk!). Her implementation then appears to work at first, but quickly degrades to this state:

                                PHYSICAL MEMORY
   0x000000 R..........K....................................................
   0x040000 KKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKK........................K
   0x080000 ...................L............RRRRRRRRRRRRRRRRRRRRRRRRCRRRRRRR
   0x0C0000 RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR
   0x100000 ....................................................LLLLLLLLLLLL
   0x140000 LLLLLLLLLL......................................................
   0x180000 ..................................................LLLLLLLLL.....
   0x1C0000 .........................L.............................LLLLL....

                             VIRTUAL ADDRESS SPACE
                          [All processes have exited]

What’s wrong? Explain briefly. You may assume that Elizabeth’s fixed fork() function correctly frees any allocated memory when a kalloc or try_map call fails, and that Elizabeth’s exit() function (not shown) is correct.

3. Page table forensics (14 points)

Here are the contents of selected 8-byte words of physical memory on an x86-64 machine running WeensyOS (specifically, a WeensyOS that has completed all phases of problem set 3).

Phys addr → word             Phys addr → word
----------------             ----------------
0x1000 → 0x2007              0x45c8 → 0xb9003
                                ⋮     (+0x1000 per word)
0x2000 → 0x3007              0x47f8 → 0xff003
                             0x4800 → 0xa005
0x3000 → 0x4007              0x4808 → 0xb005
0x3008 → 0x5007              0x4810 → 0xc005
                             0x4818 → 0xd007
0x4000 → 0x0                 0x4820 → 0x22007
0x4008 → 0x1003              0x4828 → 0x25007
0x4010 → 0x2003              0x4830 → 0x28007
0x4018 → 0x3003              0x4838 → 0x2b007
0x4020 → 0x4003              0x4840 → 0x0
   ⋮     (+0x1000 per word)     —
0x45b8 → 0xb7003             0x57f8 → 0xe007
0x45c0 → 0xb8007

A row of — marks omitted words; a ⋮ row means the pattern above it continues, each successive word 0x1000 greater than the one before. All words in physical pages 0x1000–0x5000 not shown or implied above contain 0.

QUESTION 3A. Which pages of physical memory appear likely to contain level-1 page table pages? (Hint: There are two.)

QUESTION 3B. Which pages of physical memory appear likely to contain level-2, level-3, and level-4 page table pages?

QUESTION 3C. These page table pages are part of a page table belonging to one of the p-allocator processes that you get when running make run. Which one: process 1, process 2, process 3, or process 4? Explain your reasoning briefly.

QUESTION 3D. How big does this process’s code segment appear to be?

QUESTION 3E. How big does this process’s heap segment appear to be?

QUESTION 3F. Assume that the word stored in physical address 0x4020 was 0x4007, not 0x4003. Explain how the process could leverage this mistake to take over the kernel (for instance, by causing the next system call to execute instructions of the process’s choosing).

QUESTION 3G. Which of the following actions by a WeensyOS process should cause a protected control transfer to the kernel? List all that apply.

  1. Executing the syscall instruction.
  2. Reading a byte from the console page at 0xB8000.
  3. Writing a byte to 0x40000 (KERNEL_START_ADDR).
  4. Executing movq %rax, %cr3.
  5. Executing cli (the instruction that disables interrupts).
  6. Executing a jmp to an address in the process’s own code segment.
  7. Dividing by zero.
  8. Executing an lea instruction while %rsp is a null pointer (0).