Skip to content

Commit

Permalink
Add multi cpu support
Browse files Browse the repository at this point in the history
  • Loading branch information
SimoneDutto committed Jun 19, 2020
1 parent 920cc07 commit 5ed4089
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 9 deletions.
Binary file modified os161-base-2.0.2/build/userland/testbin/palin/palin
Binary file not shown.
Binary file modified os161-base-2.0.2/build/userland/testbin/palin/palin.o
Binary file not shown.
9 changes: 8 additions & 1 deletion os161-base-2.0.2/kern/include/thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
#include <array.h>
#include <spinlock.h>
#include <threadlist.h>

#include "opt-monitor.h"
struct cpu;

/* get machine-dependent defs */
Expand Down Expand Up @@ -144,6 +144,13 @@ int thread_fork(const char *name, struct proc *proc,
void (*func)(void *, unsigned long),
void *data1, unsigned long data2);


#if OPT_MONITOR
int thread_fork_tocpu(const char *name, struct proc *proc,
void (*func)(void *, unsigned long),
void *data1, unsigned long data2);

#endif
/*
* Cause the current thread to exit.
* Interrupts need not be disabled.
Expand Down
4 changes: 2 additions & 2 deletions os161-base-2.0.2/kern/main/menu.c
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ common_prog(int nargs, char **args)
return ENOMEM;
}

result = thread_fork(args[0] /* thread name */,
result = thread_fork_tocpu(args[0] /* thread name */,
proc /* new process */,
cmd_progthread /* thread function */,
args /* thread arg */, nargs /* thread arg */);
Expand Down Expand Up @@ -172,7 +172,7 @@ monitor_prog(int nargs, char **args)
}
proc_setmonitor(proc, 0);
monitor_addproc(proc);
result = thread_fork(args[0] /* thread name */,
result = thread_fork_tocpu(args[0] /* thread name */,
proc /* new process */,
cmd_progthread /* thread function */,
args /* thread arg */, nargs /* thread arg */);
Expand Down
10 changes: 7 additions & 3 deletions os161-base-2.0.2/kern/proc/monitor.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ static struct _monitor{
int active;
struct proc *proc[MAX_PROC+1];
int last_i;
int count;
struct spinlock lk;
} monitor;

Expand All @@ -45,7 +46,7 @@ void monitor_check(void *ptr, unsigned long nargs){

while(1){
int i = 0;
while(i < monitor.last_i){
while(i < monitor.last_i && monitor.count != 0){
spinlock_acquire(&monitor.lk);
struct proc* p = monitor.proc[i];
spinlock_release(&monitor.lk);
Expand All @@ -54,15 +55,15 @@ void monitor_check(void *ptr, unsigned long nargs){
continue;
}
evaluate_features();
uint32_t n = random()%10000;
//uint32_t n = random()%10000;
uint32_t n = 1;
if(n == 1){
spinlock_acquire(&p->p_lock);
p->faulty = 1;
spinlock_release(&p->p_lock);
n = 0;
}
i++;
//kprintf("Ciao sono il thread di controllo\n");
}
thread_yield();
}
Expand Down Expand Up @@ -121,6 +122,7 @@ int monitor_start(void){
spinlock_init(&monitor.lk);
monitor.active=1;
monitor.last_i=0;
monitor.count=0;
char args[20]="dummy";
// Open fake driver files
int result = vfs_open((char *) ds_names[0], O_RDONLY, 0, &v);
Expand Down Expand Up @@ -150,6 +152,7 @@ int monitor_addproc(struct proc* proc){
if (monitor.proc[i] == NULL) {
monitor.proc[i] = proc;
monitor.last_i = i+1;
monitor.count++;
break;
}
i++;
Expand All @@ -163,6 +166,7 @@ int monitor_removeproc(struct proc* p){
for(i=0;i<MAX_PROC+1;i++){
if(monitor.proc[i] == p){
monitor.proc[i] = NULL;
monitor.count--;
break;
}

Expand Down
65 changes: 63 additions & 2 deletions os161-base-2.0.2/kern/thread/thread.c
Original file line number Diff line number Diff line change
Expand Up @@ -514,8 +514,6 @@ thread_fork(const char *name,
/*
* Now we clone various fields from the parent thread.
*/

/* Thread subsystem fields */
newthread->t_cpu = curthread->t_cpu;

/* Attach the new thread to its process */
Expand Down Expand Up @@ -545,6 +543,68 @@ thread_fork(const char *name,
return 0;
}

#if OPT_MONITOR

int
thread_fork_tocpu(const char *name,
struct proc *proc,
void (*entrypoint)(void *data1, unsigned long data2),
void *data1, unsigned long data2)
{
struct thread *newthread;
int result;
struct cpu *c;
unsigned numcpus;

newthread = thread_create(name);
if (newthread == NULL) {
return ENOMEM;
}

/* Allocate a stack */
newthread->t_stack = kmalloc(STACK_SIZE);
if (newthread->t_stack == NULL) {
thread_destroy(newthread);
return ENOMEM;
}
thread_checkstack_init(newthread);

/*
* Now we clone various fields from the parent thread.
*/


numcpus = cpuarray_num(&allcpus);
c = cpuarray_get(&allcpus, numcpus-1);
newthread->t_cpu = c;

/* Attach the new thread to its process */
if (proc == NULL) {
proc = curthread->t_proc;
}
result = proc_addthread(proc, newthread);
if (result) {
/* thread_destroy will clean up the stack */
thread_destroy(newthread);
return result;
}

/*
* Because new threads come out holding the cpu runqueue lock
* (see notes at bottom of thread_switch), we need to account
* for the spllower() that will be done releasing it.
*/
newthread->t_iplhigh_count++;

/* Set up the switchframe so entrypoint() gets called */
switchframe_init(newthread, entrypoint, data1, data2);

/* Lock the current cpu's run queue and make the new thread runnable */
thread_make_runnable(newthread, false);

return 0;
}
#endif
/*
* High level, machine-independent context switch code.
*
Expand Down Expand Up @@ -722,6 +782,7 @@ thread_switch(threadstate_t newstate, struct wchan *wc, struct spinlock *lk)
/* Turn interrupts back on. */
splx(spl);
#if OPT_MONITOR

int fault = proc_fault();

if(fault){
Expand Down
2 changes: 1 addition & 1 deletion os161-base-2.0.2/userland/testbin/palin/palin.c
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ main(void)
{
char *start, *end;

printf("Welcome to the palindrome tester!\n");
printf("Welcome to the palindrome tester by Simone!\n");
printf("I will take a large palindrome and test it.\n");
printf("Here it is:\n");
printf("%s\n", palindrome);
Expand Down

0 comments on commit 5ed4089

Please sign in to comment.