Skip to content

Commit

Permalink
基本完成PA4
Browse files Browse the repository at this point in the history
  • Loading branch information
cs-qyzhang committed Dec 11, 2019
1 parent 9b0db57 commit 5844622
Show file tree
Hide file tree
Showing 13 changed files with 110 additions and 38 deletions.
3 changes: 3 additions & 0 deletions nanos-lite/include/proc.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "memory.h"

#define STACK_SIZE (8 * PGSIZE)
#define MAX_NR_PROC 4

typedef union {
uint8_t stack[STACK_SIZE] PG_ALIGN;
Expand All @@ -17,6 +18,8 @@ typedef union {
} PCB;

extern PCB *current;
extern PCB *fg_pcb;
extern PCB pcb[MAX_NR_PROC];
extern void naive_uload(PCB *pcb, const char *filename);
extern void context_uload(PCB *pcb, const char *filename);
extern void run_proc(PCB *pcb);
Expand Down
17 changes: 17 additions & 0 deletions nanos-lite/src/device.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "common.h"
#include "proc.h"
#include <amdev.h>

extern void _yield();
Expand Down Expand Up @@ -29,6 +30,22 @@ size_t events_read(void *buf, size_t offset, size_t len) {
sprintf(buf, "t %d\n", uptime());
} else if (keycode & KEYDOWN_MASK) {
sprintf(buf, "kd %s\n", keyname[keycode & ~KEYDOWN_MASK]);
if (keyname[keycode & ~KEYDOWN_MASK][0] == 'F') {
Log("F key down!");
switch (keyname[keycode & ~KEYDOWN_MASK][1]) {
case '1':
fg_pcb = &pcb[1];
break;
case '2':
fg_pcb = &pcb[2];
break;
case '3':
fg_pcb = &pcb[3];
break;
default:
break;
}
}
} else {
sprintf(buf, "ku %s\n", keyname[keycode & ~KEYDOWN_MASK]);
}
Expand Down
9 changes: 8 additions & 1 deletion nanos-lite/src/irq.c
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
#include "common.h"
#include "am.h"

static _Context* do_event(_Event e, _Context* c) {
extern _Context* do_syscall(_Context *c);
extern _Context* schedule(_Context *prev);

switch (e.event) {
case _EVENT_IRQ_TIMER: // 接收到时钟中断时按照yield处理
#ifdef DEBUG
Log("EVENT_IRQ_TIMER");
#endif
case _EVENT_YIELD:
printf("yield!\n");
#ifdef DEBUG
Log("yield!");
#endif
return schedule(c);

case _EVENT_SYSCALL:
Expand Down
4 changes: 3 additions & 1 deletion nanos-lite/src/mm.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ void free_page(void *p) {

/* The brk() system call handler. */
int mm_brk(uintptr_t new_brk) {
printf("mm_brk: %x, max_brk: %x\n", (uint32_t)new_brk, (uint32_t)current->max_brk);
#ifdef DEBUG
Log("mm_brk: %x, max_brk: %x\n", (uint32_t)new_brk, (uint32_t)current->max_brk);
#endif
// 首次调用记录max_brk
if (current->max_brk == 0) {
current->max_brk = (new_brk & 0xfff) ? ((new_brk & ~0xfff) + PGSIZE) : new_brk;
Expand Down
21 changes: 10 additions & 11 deletions nanos-lite/src/proc.c
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
#include "proc.h"

#define MAX_NR_PROC 4

static PCB pcb[MAX_NR_PROC] __attribute__((used)) = {};
PCB pcb[MAX_NR_PROC] __attribute__((used)) = {};
static PCB pcb_boot = {};
PCB *current = NULL;
PCB *fg_pcb = NULL;

void switch_boot_pcb() {
current = &pcb_boot;
Expand Down Expand Up @@ -32,25 +31,25 @@ void init_proc() {

//switch_boot_pcb();
current = &pcb[0];
fg_pcb = &pcb[1];

Log("Initializing processes...");

// load program here
//naive_uload(NULL, "/bin/dummy");
context_uload(&pcb[0], "/bin/init");
context_uload(&pcb[1], "/bin/hello");
context_uload(&pcb[0], "/bin/hello");
context_uload(&pcb[1], "/bin/pal");
context_uload(&pcb[2], "/bin/pal");
context_uload(&pcb[3], "/bin/pal");
run_proc(&pcb[0]);
}

_Context* schedule(_Context *prev) {
//static int is_boot = 0;
//const int boot_pcb_nice = 2000;
static uint32_t cnt = 0;
const int fg_nice = 2000;

printf("schedule!\n");
current->cp = prev;
//current = &pcb[0];
//current = (is_boot++ % boot_pcb_nice) ? &pcb_boot : &pcb[0];
current = (current == &pcb[0] ? &pcb[1] : &pcb[0]);
current = (cnt++ % fg_nice) ? fg_pcb : &pcb[0];

return current->cp;
}
4 changes: 4 additions & 0 deletions nemu/src/cpu/cpu.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,9 @@ vaddr_t exec_once(void) {
isa_exec(&decinfo.seq_pc);
update_pc();

// 中断检测
extern bool isa_query_intr(void);
if (isa_query_intr()) update_pc();

return decinfo.seq_pc;
}
1 change: 1 addition & 0 deletions nemu/src/device/intr.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "nemu.h"

void dev_raise_intr() {
cpu.INTR = true;
}
43 changes: 24 additions & 19 deletions nemu/src/isa/x86/include/isa/reg.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,25 +37,28 @@ typedef struct {

vaddr_t pc;

struct {
uint32_t CF : 1;
uint32_t : 1;
uint32_t PF : 1;
uint32_t : 1;
uint32_t AF : 1;
uint32_t : 1;
uint32_t ZF : 1;
uint32_t SF : 1;
uint32_t TF : 1;
uint32_t IF : 1;
uint32_t DF : 1;
uint32_t OF : 1;
uint32_t IOPL : 2;
uint32_t NT : 1;
uint32_t : 1;
uint32_t RF : 1;
uint32_t VM : 1;
uint32_t : 14;
union{
struct {
uint32_t CF : 1;
uint32_t : 1;
uint32_t PF : 1;
uint32_t : 1;
uint32_t AF : 1;
uint32_t : 1;
uint32_t ZF : 1;
uint32_t SF : 1;
uint32_t TF : 1;
uint32_t IF : 1;
uint32_t DF : 1;
uint32_t OF : 1;
uint32_t IOPL : 2;
uint32_t NT : 1;
uint32_t : 1;
uint32_t RF : 1;
uint32_t VM : 1;
uint32_t : 14;
};
uint32_t val;
} eflags;

struct {
Expand All @@ -68,6 +71,8 @@ typedef struct {

uint16_t CS, SS, DS, ES, FS, GS;

bool INTR;

} CPU_state;

static inline int check_reg_index(int index) {
Expand Down
3 changes: 2 additions & 1 deletion nemu/src/isa/x86/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@ const long isa_default_img_size = sizeof(isa_default_img);
static void restart() {
/* Set the initial program counter. */
cpu.pc = PC_START;
memset(&cpu.eflags, 0x02, sizeof(cpu.eflags));
cpu.eflags.val = 0x02;
cpu.CS = 8;
cpu.SS = 0;
cpu.DS = 0;
cpu.ES = 0;
cpu.FS = 0;
cpu.GS = 0;
cpu.cr0.val = 0x60000011; // 启动时关闭分页
cpu.INTR = false;
}

void init_isa(void) {
Expand Down
8 changes: 8 additions & 0 deletions nemu/src/isa/x86/intr.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include "rtl/rtl.h"
#include "isa/rtl.h"

#define IRQ_TIMER 0x20

void raise_intr(uint32_t NO, vaddr_t ret_addr) {
/* XXX: Trigger an interrupt/exception with ``NO''.
* That is, use ``NO'' to index the IDT.
Expand All @@ -9,6 +11,7 @@ void raise_intr(uint32_t NO, vaddr_t ret_addr) {

assert((NO + 1) * 8 <= cpu.IDTR.limit);
rtl_push((rtlreg_t *)&cpu.eflags);
cpu.eflags.IF = 0; // 关中断
rtl_li(&tmp, cpu.CS);
rtl_push(&tmp);
rtl_push(&ret_addr);
Expand All @@ -32,5 +35,10 @@ void raise_intr(uint32_t NO, vaddr_t ret_addr) {
}

bool isa_query_intr(void) {
if (cpu.eflags.IF & cpu.INTR) {
cpu.INTR = false;
raise_intr(IRQ_TIMER, cpu.pc);
return true;
}
return false;
}
25 changes: 24 additions & 1 deletion nexus-am/am/include/arch/x86-nemu.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,30 @@ struct _Context {
struct _AddressSpace *as;
uint32_t edi, esi, ebp, esp, ebx, edx, ecx, eax;
uint32_t irq;
uint32_t eip, cs, eflags;
uint32_t eip, cs;
union{
struct {
uint32_t CF : 1;
uint32_t : 1;
uint32_t PF : 1;
uint32_t : 1;
uint32_t AF : 1;
uint32_t : 1;
uint32_t ZF : 1;
uint32_t SF : 1;
uint32_t TF : 1;
uint32_t IF : 1;
uint32_t DF : 1;
uint32_t OF : 1;
uint32_t IOPL : 2;
uint32_t NT : 1;
uint32_t : 1;
uint32_t RF : 1;
uint32_t VM : 1;
uint32_t : 14;
};
uint32_t val;
} eflags;
};

// XXX https://en.wikibooks.org/wiki/X86_Assembly/Interfacing_with_Linux
Expand Down
7 changes: 4 additions & 3 deletions nexus-am/am/src/x86/nemu/cte.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ _Context* __am_irq_handle(_Context *c) {
if (user_handler) {
_Event ev = {0};
switch (c->irq) {
case 0x80: ev.event = _EVENT_SYSCALL; break;
case 0x81: ev.event = _EVENT_YIELD; break;
default: ev.event = _EVENT_ERROR; break;
case 0x20: ev.event = _EVENT_IRQ_TIMER; break;
case 0x80: ev.event = _EVENT_SYSCALL; break;
case 0x81: ev.event = _EVENT_YIELD; break;
default: ev.event = _EVENT_ERROR; break;
}

next = user_handler(ev, c);
Expand Down
3 changes: 2 additions & 1 deletion nexus-am/am/src/x86/nemu/vme.c
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,9 @@ _Context *_ucontext(_AddressSpace *as, _Area ustack, _Area kstack, void *entry,
memset(context, 0x00, sizeof(_Context) + 0x20);
context->cs = 8;
context->eip = (uint32_t)entry;
memset(&context->eflags, 0x02, sizeof(context->eflags));
context->eflags.val = 0x02;
context->as = as;
context->eflags.IF = 1;

return context;
}
Expand Down

0 comments on commit 5844622

Please sign in to comment.