Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ AS = $(TOOLPREFIX)gas
LD = $(TOOLPREFIX)ld
OBJCOPY = $(TOOLPREFIX)objcopy
OBJDUMP = $(TOOLPREFIX)objdump
CFLAGS = -fno-pic -static -fno-builtin -fno-strict-aliasing -O2 -Wall -MD -ggdb -m32 -Werror -fno-omit-frame-pointer
CFLAGS = -fno-pic -static -fno-builtin -fno-strict-aliasing -O0 -Wall -MD -ggdb -m32 -fno-omit-frame-pointer
#CFLAGS = -fno-pic -static -fno-builtin -fno-strict-aliasing -O2 -Wall -MD -ggdb -m32 -Werror -fno-omit-frame-pointer
#CFLAGS = -fno-pic -static -fno-builtin -fno-strict-aliasing -fvar-tracking -fvar-tracking-assignments -O0 -g -Wall -MD -gdwarf-2 -m32 -Werror -fno-omit-frame-pointer
CFLAGS += $(shell $(CC) -fno-stack-protector -E -x c /dev/null >/dev/null 2>&1 && echo -fno-stack-protector)
ASFLAGS = -m32 -gdwarf-2 -Wa,-divide
Expand Down Expand Up @@ -179,6 +180,7 @@ UPROGS=\
_test_master\
_test_mlfq\
_test_stride\
_threadtest\

fs.img: mkfs README $(UPROGS)
./mkfs fs.img README $(UPROGS)
Expand Down
4 changes: 4 additions & 0 deletions defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,10 @@ void push_queue(struct proc* p, int lev);
void pop_queue(struct proc* p);
void push_stride(struct proc* p);
void pop_stride(struct proc* p);
int clone(thread_t *, void *(*)(void *), void *, void*);
void thread_exit(void *);
int thread_join(thread_t, void **);


// swtch.S
void swtch(struct context**, struct context*);
Expand Down
80 changes: 78 additions & 2 deletions proc.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@ ChangeContext(struct cpu* c, struct proc* p) {
switchuvm(p);
p->state = RUNNING;

// Do Context Switching
swtch(&(c->scheduler), p->context);
switchkvm();

switchkvm();
c->proc = 0;
}

Expand Down Expand Up @@ -176,6 +177,8 @@ allocproc(void)
found:
p->state = EMBRYO;
p->pid = nextpid++;
p->tid = 0;
p->isThread = 0;

release(&ptable.lock);

Expand Down Expand Up @@ -210,8 +213,77 @@ allocproc(void)
return p;
}

int
clone(thread_t *thread, void *(*start_routine)(void *), void *arg, void* stack)
{
int i;
struct proc *np;
struct proc *curproc = myproc();
uint ustack[3];
void* sp;

// Allocate process.
if((np = allocproc()) == 0){
return -1;
}

*thread = nextpid;
curproc->tid++;

np->isThread = 1;
np->sz = curproc->sz;
np->parent = curproc;
*np->tf = *curproc->tf;

// Clear %eax so that fork returns 0 in the child.
np->tf->eax = 0;

np->pgdir = curproc->pgdir;

// Allocate two pages at the next page boundary.
// Make the first inaccessible. Use the second as the user stack.

ustack[0] = 0xFFFFFFFF;
ustack[1] = (uint)arg;

sp = stack + PGSIZE;
sp -= 8;

if(copyout(np->pgdir, (uint)sp, ustack, 8) < 0)
goto bad;

np->tf->esp = (uint)sp;
np->tf->eip = (uint)start_routine;

for(i = 0; i < NOFILE; i++)
if(curproc->ofile[i])
np->ofile[i] = filedup(curproc->ofile[i]);
np->cwd = idup(curproc->cwd);
safestrcpy(np->name, "thread", sizeof(curproc->name));

acquire(&ptable.lock);
np->state = RUNNABLE;

release(&ptable.lock);
cprintf("sex\n");
return 0;

bad:
panic("bad state on thread_create");
return -1;

}

// TODO : Implement
void
thread_exit(void* retval);

// TODO : Implement
int
thread_join(thread_t thread, void **retval);

//PAGEBREAK: 32
// Set up first user process.
// Set st user process.
void
userinit(void)
{
Expand Down Expand Up @@ -256,6 +328,7 @@ growproc(int n)
uint sz;
struct proc *curproc = myproc();

//acquire(&(curproc->sz_lock));
sz = curproc->sz;
if(n > 0){
if((sz = allocuvm(curproc->pgdir, sz, sz + n)) == 0)
Expand All @@ -265,6 +338,8 @@ growproc(int n)
return -1;
}
curproc->sz = sz;
//release(&(curproc->sz_lock));

switchuvm(curproc);
return 0;
}
Expand All @@ -291,6 +366,7 @@ fork(void)
np->state = UNUSED;
return -1;
}

np->sz = curproc->sz;
np->parent = curproc;
*np->tf = *curproc->tf;
Expand Down
6 changes: 6 additions & 0 deletions proc.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#include "spinlock.h"

// Per-CPU state
struct cpu {
uchar apicid; // Local APIC ID
Expand Down Expand Up @@ -41,6 +43,8 @@ struct proc {
char *kstack; // Bottom of kernel stack for this process
enum procstate state; // Process state
int pid; // Process ID
int tid; // Thread ID
int isThread;
struct proc *parent; // Parent process
struct trapframe *tf; // Trap frame for current syscall
struct context *context; // swtch() here to run process
Expand All @@ -56,6 +60,8 @@ struct proc {
int isStride; // if 1, this proc is stride
int time_allotment; // ticks for check whether we should drop the priority for mlfq
int rtick_for_boost; // total tick proc run in one boosting cycle

struct spinlock sz_lock;
};


Expand Down
4 changes: 4 additions & 0 deletions spinlock.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#ifndef _SPINLOCK_H_
#define _SPINLOCK_H_

// Mutual exclusion lock.
struct spinlock {
uint locked; // Is the lock held?
Expand All @@ -9,3 +12,4 @@ struct spinlock {
// that locked the lock.
};

#endif
Empty file added sys_thread_create
Empty file.
6 changes: 6 additions & 0 deletions syscall.c
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ extern int sys_set_cpu_share(void);
extern int sys_alarm(void);
extern int sys_yield(void);
extern int sys_getlev(void);
extern int sys_clone(void);
extern int sys_thread_exit(void);
extern int sys_thread_join(void);

static int (*syscalls[])(void) = {
[SYS_fork] sys_fork,
Expand Down Expand Up @@ -138,6 +141,9 @@ static int (*syscalls[])(void) = {
[SYS_alarm] sys_alarm,
[SYS_yield] sys_yield,
[SYS_getlev] sys_getlev,
[SYS_clone] sys_clone,
[SYS_thread_exit] sys_thread_exit,
[SYS_thread_join] sys_thread_join,
};

void
Expand Down
3 changes: 3 additions & 0 deletions syscall.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,6 @@
#define SYS_alarm 25
#define SYS_yield 26
#define SYS_getlev 27
#define SYS_clone 28
#define SYS_thread_exit 29
#define SYS_thread_join 30
49 changes: 49 additions & 0 deletions sysproc.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,55 @@ sys_fork(void)
return fork();
}

int
sys_clone(void)
{
int n;
void *thread;
void *start_routine;
void *arg;
void *stack;

if(argint(0, &n) < 0) {
cprintf("in sys_thread_create\n");
return -1;
}
thread = (void*) n;

if(argint(1, &n) < 0) {
cprintf("in sys_thread_create\n");
return -1;
}
start_routine = (void*) n;


if(argint(2, &n) < 0) {
cprintf("in sys_thread_create\n");
return -1;
}
arg = (void*) n;

if(argint(3, &n) < 0) {
cprintf("in sys_thread_create\n");
return -1;
}
stack = (void*) n;

return clone(thread, start_routine, arg, stack);
}

int
sys_thread_exit(void)
{
return 0;
}

int
sys_thread_join(void)
{
return 0;
}

int
sys_exit(void)
{
Expand Down
Loading