Skip to content

Commit

Permalink
add more comments
Browse files Browse the repository at this point in the history
  • Loading branch information
fxlin committed Feb 29, 2024
1 parent 29a4228 commit cfc62c3
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
3 changes: 3 additions & 0 deletions src/exp6/include/sched.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ extern void switch_to(struct task_struct* next);
extern void cpu_switch_to(struct task_struct* prev, struct task_struct* next);
extern void exit_process(void);

// the initial values for task_struct that belongs to the init task. see sched.c
// NB: init task is in kernel, only has kernel mapping (ttbr1)
// no user mapping (ttbr0, mm->pgd=0)
#define INIT_TASK \
/*cpu_context*/ { { 0,0,0,0,0,0,0,0,0,0,0,0,0}, \
/* state etc */ 0,0,15, 0, PF_KTHREAD, \
Expand Down
15 changes: 13 additions & 2 deletions src/exp6/src/fork.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ int copy_process(unsigned long clone_flags, unsigned long fn, unsigned long arg)
p->state = TASK_RUNNING;
p->counter = p->priority;
p->preempt_count = 1; //disable preemtion until schedule_tail

// @page is 0-filled, many fields (e.g. mm.pgd) are implicitly init'd

p->cpu_context.pc = (unsigned long)ret_from_fork;
p->cpu_context.sp = (unsigned long)childregs;
int pid = nr_tasks++;
Expand All @@ -41,7 +42,11 @@ int copy_process(unsigned long clone_flags, unsigned long fn, unsigned long arg)
}


/* @start: a pointer to the beginning of the user code (to be copied to the new task),
/*
Populate pt_regs for returning to user space (via kernel_exit) for the 1st time.
Note that the actual switch will not happen until kernel_exit.
@start: a pointer to the beginning of the user code (to be copied to the new task),
@size: size of the area
@pc: offset of the startup function inside the area
*/
Expand All @@ -51,7 +56,10 @@ int move_to_user_mode(unsigned long start, unsigned long size, unsigned long pc)
struct pt_regs *regs = task_pt_regs(current);
regs->pstate = PSR_MODE_EL0t;
regs->pc = pc;
/* assumption: our toy user program will not exceed 1 page. the 2nd page will serve as the stack */
regs->sp = 2 * PAGE_SIZE;
/* only allocate 1 code page here b/c the stack page is to be mapped on demand.
this will trigger allocating the task's pgtable tree (mm.pgd) */
unsigned long code_page = allocate_user_page(current, 0);
if (code_page == 0) {
return -1;
Expand All @@ -61,6 +69,9 @@ int move_to_user_mode(unsigned long start, unsigned long size, unsigned long pc)
return 0;
}

/* get a task's saved registers, which are at the top of the task's kernel page.
these regs are saved/restored by kernel_entry()/kernel_exit().
*/
struct pt_regs * task_pt_regs(struct task_struct *tsk)
{
unsigned long p = (unsigned long)tsk + THREAD_SIZE - sizeof(struct pt_regs);
Expand Down

0 comments on commit cfc62c3

Please sign in to comment.