Part A: User Environments and Exception Handling
1. User Environments
struct used to represent an environment
struct Env {
struct Trapframe env_tf; // Saved registers
struct Env *env_link; // Next free Env
envid_t env_id; // Unique environment identifier
envid_t env_parent_id; // env_id of this env's parent
enum EnvType env_type; // Indicates special system environments
unsigned env_status; // Status of the environment
uint32_t env_runs; // Number of times environment has run
// Address space
pde_t *env_pgdir; // Kernel virtual address of page dir
};
three main global variables pertaining to environments:
struct Env *envs = NULL; // All environments
struct Env *curenv = NULL; // The current env
static struct Env *env_free_list; // Free environment list
functions used for running environments
int envid2env(envid_t envid, struct Env **env_store, bool checkperm) // Converts an envid to an env pointer.
int env_alloc(struct Env **newenv_store, envid_t parent_id) // alloc an Env from array envs, init variables in Env.
static int env_setup_vm(struct Env *e) // Initialize the KERNEL virtual memory layout for environment e. ONLY used in env_alloc()
static void region_alloc(struct Env *e, void *va, size_t len) // Allocate len bytes of physical memory for environment e, and map it at virtual address va.
void env_create(uint8_t *binary, enum EnvType type) // create env ONLY during kernel initialization, later we should use fork
static void load_icode(struct Env *e, uint8_t *binary) // only used in env_create()
void env_free(struct Env *e) // Frees env e and all memory it uses.
void env_destroy(struct Env *e)
void env_pop_tf(struct Trapframe *tf)
void env_run(struct Env *e)
summary of the creation of an environment
- Allocate an Env from array envs. Implemented by env_alloc().
- Initialize env_pgdir field of Env, actually, Initialize the virtual memory layout above UTOP including. Implemented by env_setup_vm().
- Initialize the rest fields of Env, except env_tf.tf_eip. Implemented by env_alloc().
- Load executable image into memory, initialize env_tf.tf_eip field. Implememted by load_icode().
- Allocate 1 page memory for the initial stack and map it at USTACKTOP - PGSIZE. Implemented by region_alloc().
2. Exception Handling
summary of the function chain when handle a trap
- t_*() :Defined by macros TRAPHANDLER and TRAPHANDLER_NOEC in trapentry.S.
- _alltraps() :All fiels of trap frame are saved up to here.
- trap()
- trap_dispatch()
- specific funtion corresponding to each trap
Part B: Page Faults, Breakpoints Exceptions, and System Calls
NULL