Class 22: Putting a Fork in Fork

Action Items

Project Design and Progress reviews will start next week. The submission form for updates and scheduling is due by 11:59pm Sunday.

Segmentation Violations

#include <stdio.h>
#include <stdlib.h>
#include <signal.h>

void (*signal(int signum, void (*sighandler)(int)))(int);

static int i;

void catch_segv(int sig) {
  printf("Caught segv: %d\n", sig);
  printf("i = %d\n", i);
}

int main(int argc, char **argv) {
  char *s = (char *) malloc (1);
  i = 0;

  signal(SIGSEGV, catch_segv);

  while (1) {
    s[i] = 'a';
    i++;
  }
}

When does this code produce a Segmentation Violation?

#include <stdio.h>
#include <stdlib.h>

void big_stack() {
  char BIG_DATA[8515*1024];
  int i = 3; // need this to avoid optimizations...
}

int main(int argc, char **argv) {
  big_stack();
}

Try uname -a to determine if this code will SEGV or not on your machine.

Representing Tasks

include/linux/sched.h

struct task_struct {
        volatile long state;        /* -1 unrunnable, 0 runnable, >0 stopped */
        void *stack;
        atomic_t usage;
        unsigned int flags;        /* per process flags, defined below */
        unsigned int ptrace;

        ... // 400 lines total

        int prio, static_prio, normal_prio;
        unsigned int rt_priority;
        const struct sched_class *sched_class;

    ... 

        struct mm_struct *mm, *active_mm;

    /* task state */
        int exit_state;
        int exit_code, exit_signal;
        int pdeath_signal;  /*  The signal sent when the parent dies  */
        unsigned int jobctl;        /* JOBCTL_*, siglock protected */

        /* Used for emulating ABI behavior of previous Linux versions */
        unsigned int personality;
    ...
        pid_t pid;
        pid_t tgid;

#ifdef CONFIG_CC_STACKPROTECTOR
        /* Canary value for the -fstack-protector gcc feature */
        unsigned long stack_canary;
#endif
        /*
         * pointers to (original) parent process, youngest child, younger sibling,
         * older sibling, respectively.  (p->father can be replaced with
         * p->real_parent->pid)
         */
        struct task_struct __rcu *real_parent; /* real parent process */
        struct task_struct __rcu *parent; /* recipient of SIGCHLD, wait4() reports */
        /*
         * children/sibling forms the list of my natural children
         */
        struct list_head children;        /* list of my children */
        struct list_head sibling;        /* linkage in my parent's children list */
        struct task_struct *group_leader;        /* threadgroup leader */

    ...
};

Why does the kernel need to know about GCC's -fstack-protector feature?

Linux Magic!

...

#define MINIX_SUPER_MAGIC        0x137F                /* minix v1 fs, 14 char names */
#define MINIX_SUPER_MAGIC2        0x138F                /* minix v1 fs, 30 char names */
#define MINIX2_SUPER_MAGIC        0x2468                /* minix v2 fs, 14 char names */
#define MINIX2_SUPER_MAGIC2        0x2478                /* minix v2 fs, 30 char names */
#define MINIX3_SUPER_MAGIC        0x4d5a                /* minix v3 fs, 60 char names */
...
#define STACK_END_MAGIC                0x57AC6E9D
...

How should the kernel flush the TLB?

Code

static inline void __native_flush_tlb(void)
{
        native_write_cr3(native_read_cr3());
}

Is Mark's answer correct? What unstated assumptions does it rely on?

For a more truthful account of how Mark did in his Operating Systems course, see Matt Welsh's How I almost killed Facebook.


Class 21: Virtual Memory

Action Items

Project Design and Progress reviews will start next week. The submission form for updates and scheduling will be posted soon and due by the end of this week.

Memory Isolation

How can a program be isolated in memory without hardware support?

Native Client - Native Client: A Sandbox for Portable, Untrusted x86 Native Code, IEEE Security and Privacy ("Oakland") 2009.

What are the advantages/disadvantages of hardware-based memory isolation over software-based memory isolation?

Virtual Memory

Robert C. Daley and Jack B. Dennis, Virtual Memory, Processes, and Sharing in MULTICS - Symposium on Operating System Principles (SOSP), 1967.

What must be done to switch processes on MULTICS?

Intel x86 Software Develop Manuals - Most of what I covered today is in Volume I, Chapter 3 (Basic Execution Environment) with Section 3.3 on Memory Organization. Chapter 5 is all about Protection.

Why is it useful to convert a logical address into a linear address, as is done by the segmentation unit?

Where is the Global Descriptor Table stored?

Why is a memory page in the 80386 (32-bit) processor 4K bytes?

What will this program do and why?

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv) {
  char *s = (char *) malloc (1);
  int i = 0;
  while (1) {
    printf("%d: %x\n", i, s[i]);
    i += 4;
  }
}

Challenge: Write a program that takes N as an input and produces (nearly) exactly N page faults.


Class 20: Crossing into Kernel Space


Pages

  • Challenges
  • Course Wrapup
  • Final Projects
  • Final Survey
  • Getting Started with Github
  • IRC
  • Problem Set 3 - Zhtta Server - Benchmarking
  • Project
  • Project Ideas
  • Problem Set 1 - zhttpto Web Server
  • Comments on PS1 Comments
  • Problem Set 1 Reference Solution
  • Problem Set 2 - The Good Auld Shell
  • Problem Set 3 - Zhtta Server
  • Page Removed
  • Schedule
  • Enrolling for Spring 2014
  • Syllabus
  • Using Materials
  • Using Rust for an Undergraduate OS Course
  • VirtualBox
  • Working on Github in cs4414