user/kernel stack size, clear content of ANONYMOUS mmap, changes to scheduler (CPU idle state)

This commit is contained in:
Balazs Gerofi
2012-05-10 14:20:52 +09:00
parent d679844685
commit 97d4c85195
3 changed files with 72 additions and 83 deletions

View File

@ -15,6 +15,10 @@
#define dkprintf(...) #define dkprintf(...)
#endif #endif
#define USER_STACK_NR_PAGES 4
#define KERNEL_STACK_NR_PAGES 4
extern long do_arch_prctl(unsigned long code, unsigned long address); extern long do_arch_prctl(unsigned long code, unsigned long address);
void init_process_vm(struct process_vm *vm) void init_process_vm(struct process_vm *vm)
@ -35,14 +39,15 @@ struct process *create_process(unsigned long user_pc)
{ {
struct process *proc; struct process *proc;
proc = aal_mc_alloc_pages(3, 0); proc = aal_mc_alloc_pages(KERNEL_STACK_NR_PAGES, 0);
if (!proc) if (!proc)
return NULL; return NULL;
memset(proc, 0, sizeof(struct process)); memset(proc, 0, sizeof(struct process));
aal_mc_init_user_process(&proc->ctx, &proc->uctx, aal_mc_init_user_process(&proc->ctx, &proc->uctx,
((char *)proc) + 3 * PAGE_SIZE, user_pc, 0); ((char *)proc) +
KERNEL_STACK_NR_PAGES * PAGE_SIZE, user_pc, 0);
proc->vm = (struct process_vm *)(proc + 1); proc->vm = (struct process_vm *)(proc + 1);
@ -56,18 +61,18 @@ struct process *clone_process(struct process *org, unsigned long pc,
{ {
struct process *proc; struct process *proc;
proc = aal_mc_alloc_pages(1, 0); proc = aal_mc_alloc_pages(KERNEL_STACK_NR_PAGES, 0);
memset(proc, 0, sizeof(*proc)); memset(proc, 0, KERNEL_STACK_NR_PAGES);
/* NOTE: sp is the user mode stack! */
aal_mc_init_user_process(&proc->ctx, &proc->uctx, aal_mc_init_user_process(&proc->ctx, &proc->uctx,
((char *)proc) + PAGE_SIZE, pc, sp); ((char *)proc) +
KERNEL_STACK_NR_PAGES * PAGE_SIZE, pc, sp);
memcpy(proc->uctx, org->uctx, sizeof(*org->uctx)); memcpy(proc->uctx, org->uctx, sizeof(*org->uctx));
aal_mc_modify_user_context(proc->uctx, AAL_UCR_STACK_POINTER, aal_mc_modify_user_context(proc->uctx, AAL_UCR_STACK_POINTER, sp);
sp); aal_mc_modify_user_context(proc->uctx, AAL_UCR_PROGRAM_COUNTER, pc);
aal_mc_modify_user_context(proc->uctx, AAL_UCR_PROGRAM_COUNTER,
pc);
aal_atomic_inc(&org->vm->refcount); aal_atomic_inc(&org->vm->refcount);
proc->vm = org->vm; proc->vm = org->vm;
@ -131,16 +136,17 @@ int add_process_memory_range(struct process *process,
} }
#define NR_STACK_PAGES 2
void init_process_stack(struct process *process) void init_process_stack(struct process *process)
{ {
char *stack = aal_mc_alloc_pages(NR_STACK_PAGES, 0); char *stack = aal_mc_alloc_pages(USER_STACK_NR_PAGES, 0);
unsigned long *p = (unsigned long *)(stack + (NR_STACK_PAGES * PAGE_SIZE)); unsigned long *p = (unsigned long *)(stack +
(USER_STACK_NR_PAGES * PAGE_SIZE));
memset(stack, 0, NR_STACK_PAGES * PAGE_SIZE); memset(stack, 0, USER_STACK_NR_PAGES * PAGE_SIZE);
add_process_memory_range(process, USER_END - (NR_STACK_PAGES * PAGE_SIZE), add_process_memory_range(process, USER_END -
(USER_STACK_NR_PAGES * PAGE_SIZE),
USER_END, USER_END,
virt_to_phys(stack), VR_STACK); virt_to_phys(stack), VR_STACK);
@ -158,7 +164,8 @@ void init_process_stack(struct process *process)
aal_mc_modify_user_context(process->uctx, AAL_UCR_STACK_POINTER, aal_mc_modify_user_context(process->uctx, AAL_UCR_STACK_POINTER,
USER_END - sizeof(unsigned long) * 9); USER_END - sizeof(unsigned long) * 9);
process->vm->region.stack_end = USER_END; process->vm->region.stack_end = USER_END;
process->vm->region.stack_start = USER_END - (NR_STACK_PAGES * PAGE_SIZE); process->vm->region.stack_start = USER_END -
(USER_STACK_NR_PAGES * PAGE_SIZE);
} }
@ -181,14 +188,18 @@ unsigned long extend_process_region(struct process *proc,
aligned_new_end = (address + PAGE_SIZE - 1) & PAGE_MASK; aligned_new_end = (address + PAGE_SIZE - 1) & PAGE_MASK;
p = allocate_pages((aligned_new_end - aligned_end) >> PAGE_SHIFT, p = allocate_pages((aligned_new_end - aligned_end) >> PAGE_SHIFT, 0);
0);
if (!p) { if (!p) {
return end; return end;
} }
/* Clear content! */
memset(p, 0, aligned_new_end - aligned_end);
add_process_memory_range(proc, aligned_end, aligned_new_end, add_process_memory_range(proc, aligned_end, aligned_new_end,
virt_to_phys(p), 0); virt_to_phys(p), 0);
return address; return address;
} }
@ -243,10 +254,12 @@ static void idle(void)
//unsigned int flags; //unsigned int flags;
//flags = aal_mc_spinlock_lock(&cpu_status_lock); //flags = aal_mc_spinlock_lock(&cpu_status_lock);
//aal_mc_spinlock_unlock(&cpu_status_lock, flags); //aal_mc_spinlock_unlock(&cpu_status_lock, flags);
cpu_local_var(status) = CPU_STATUS_IDLE;
while (1) { while (1) {
cpu_enable_interrupt(); cpu_enable_interrupt();
schedule(); schedule();
cpu_local_var(status) = CPU_STATUS_IDLE; //cpu_local_var(status) = CPU_STATUS_IDLE;
cpu_halt(); cpu_halt();
} }
} }
@ -321,14 +334,14 @@ void schedule(void)
kprintf("[%d] schedule: tlsblock_base: 0x%lX\n", kprintf("[%d] schedule: tlsblock_base: 0x%lX\n",
aal_mc_get_processor_id(), next->thread.tlsblock_base); aal_mc_get_processor_id(), next->thread.tlsblock_base);
do_arch_prctl(ARCH_SET_FS, next->thread.tlsblock_base);
if (next != &cpu_local_var(idle)) /* Set up new TLS.. */
cpu_local_var(status) = CPU_STATUS_RUNNING; do_arch_prctl(ARCH_SET_FS, next->thread.tlsblock_base);
if (prev) { if (prev) {
aal_mc_switch_context(&prev->ctx, &next->ctx); aal_mc_switch_context(&prev->ctx, &next->ctx);
} else { }
else {
aal_mc_switch_context(NULL, &next->ctx); aal_mc_switch_context(NULL, &next->ctx);
} }
} }
@ -386,12 +399,13 @@ void runq_add_proc(struct process *proc, int cpu_id)
__runq_add_proc(proc, cpu_id); __runq_add_proc(proc, cpu_id);
aal_mc_spinlock_unlock(&(v->runq_lock), irqstate); aal_mc_spinlock_unlock(&(v->runq_lock), irqstate);
/* /* Kick scheduler */
if (cpu != this_cpu) if (cpu_id != aal_mc_get_processor_id())
xcall_reschedule(cpu); aal_mc_interrupt_cpu(
*/ get_x86_cpu_local_variable(cpu_id)->apic_id, 0xd1);
} }
/* NOTE: shouldn't remove a running process! */
void runq_del_proc(struct process *proc, int cpu_id) void runq_del_proc(struct process *proc, int cpu_id)
{ {
struct cpu_local_var *v = get_cpu_local_var(cpu_id); struct cpu_local_var *v = get_cpu_local_var(cpu_id);
@ -399,7 +413,11 @@ void runq_del_proc(struct process *proc, int cpu_id)
irqstate = aal_mc_spinlock_lock(&(v->runq_lock)); irqstate = aal_mc_spinlock_lock(&(v->runq_lock));
list_del(&proc->sched_list); list_del(&proc->sched_list);
++v->runq_len; --v->runq_len;
if (!v->runq_len)
get_cpu_local_var(cpu_id)->status = CPU_STATUS_IDLE;
aal_mc_spinlock_unlock(&(v->runq_lock), irqstate); aal_mc_spinlock_unlock(&(v->runq_lock), irqstate);
} }

View File

@ -30,8 +30,6 @@
#define dkprintf(...) #define dkprintf(...)
#endif #endif
static aal_spinlock_t sysc_lock = { 0 };
static aal_atomic_t pid_cnt = AAL_ATOMIC_INIT(1024); static aal_atomic_t pid_cnt = AAL_ATOMIC_INIT(1024);
int memcpy_async(unsigned long dest, unsigned long src, int memcpy_async(unsigned long dest, unsigned long src,
@ -295,7 +293,7 @@ SYSCALL_DECLARE(mmap)
unsigned long address, ret; unsigned long address, ret;
struct vm_regions *region = &cpu_local_var(current)->vm->region; struct vm_regions *region = &cpu_local_var(current)->vm->region;
/* anonymous */ /* MAP_ANONYMOUS */
if (aal_mc_syscall_arg3(ctx) & 0x22) { if (aal_mc_syscall_arg3(ctx) & 0x22) {
ret = region->map_end; ret = region->map_end;
address = region->map_end + aal_mc_syscall_arg1(ctx); address = region->map_end + aal_mc_syscall_arg1(ctx);
@ -415,49 +413,6 @@ SYSCALL_DECLARE(arch_prctl)
aal_mc_syscall_arg1(ctx)); aal_mc_syscall_arg1(ctx));
} }
#if 0
long sys_arch_prctl(int n, aal_mc_user_context_t *ctx)
{
unsigned long code = aal_mc_syscall_arg0(ctx);
unsigned long address = aal_mc_syscall_arg1(ctx);
switch (code) {
case 0x1002:
return aal_mc_arch_set_special_register(AAL_ASR_X86_FS,
address);
case 0x1003:
return aal_mc_arch_get_special_register(AAL_ASR_X86_FS,
(unsigned long *)
address);
}
return -EINVAL;
}
SYSCALL_DECLARE(clone)
{
/* Clone a new thread */
struct process *new;
struct syscall_request request AAL_DMA_ALIGN;
new = clone_process(cpu_local_var(current), aal_mc_syscall_pc(ctx),
aal_mc_syscall_arg1(ctx));
/* XXX Assign new pid! */
new->pid = cpu_local_var(current)->pid;
dkprintf("Cloned: %p \n", new);
aal_mc_syscall_ret(new->uctx) = 0;
/* Hope it is scheduled after... :) */
request.number = n;
request.args[0] = (unsigned long)new;
/* Sync */
do_syscall(&request, ctx);
dkprintf("Clone ret.\n");
return new->pid;
}
#endif
SYSCALL_DECLARE(clone) SYSCALL_DECLARE(clone)
{ {
@ -519,15 +474,21 @@ SYSCALL_DECLARE(clone)
} }
aal_mc_syscall_ret(new->uctx) = 0; aal_mc_syscall_ret(new->uctx) = 0;
runq_add_proc(new, cpuid);
//get_cpu_local_var(cpuid)->next = new;
//aal_mc_spinlock_unlock(&cpu_status_lock, flags);
dkprintf("clone: kicking scheduler!\n"); dkprintf("clone: kicking scheduler!\n");
aal_mc_interrupt_cpu(get_x86_cpu_local_variable(cpuid)->apic_id, 0xd1); runq_add_proc(new, cpuid);
//while (1) { cpu_halt(); } //while (1) { cpu_halt(); }
#if 0
aal_mc_syscall_ret(new->uctx) = 0;
/* Hope it is scheduled after... :) */
request.number = n;
request.args[0] = (unsigned long)new;
/* Sync */
do_syscall(&request, ctx);
dkprintf("Clone ret.\n");
#endif
return new->pid; return new->pid;
} }
@ -653,8 +614,8 @@ SYSCALL_DECLARE(getrlimit)
case RLIMIT_STACK: case RLIMIT_STACK:
dkprintf("[%d] getrlimit() RLIMIT_STACK\n", aal_mc_get_processor_id()); dkprintf("[%d] getrlimit() RLIMIT_STACK\n", aal_mc_get_processor_id());
rlm->rlim_cur = (1024*1024); rlm->rlim_cur = (128*4096); /* Linux provides 8MB */
rlm->rlim_max = (16384*1024); rlm->rlim_max = (1024*1024*1024);
ret = 0; ret = 0;
break; break;

View File

@ -95,8 +95,9 @@ struct program_load_desc *load_elf(FILE *fp)
desc->sections[j].offset = phdr.p_offset; desc->sections[j].offset = phdr.p_offset;
desc->sections[j].len = phdr.p_memsz; desc->sections[j].len = phdr.p_memsz;
__dprintf("%d: %lx, %lx, %lx, %lx\n", __dprintf("%d: (%s) %lx, %lx, %lx, %lx\n",
j, desc->sections[j].vaddr, j, (phdr.p_type == PT_LOAD ? "PT_LOAD" : "PT_TLS"),
desc->sections[j].vaddr,
desc->sections[j].filesz, desc->sections[j].filesz,
desc->sections[j].offset, desc->sections[j].offset,
desc->sections[j].len); desc->sections[j].len);
@ -414,6 +415,15 @@ int main_loop(int fd, int cpu)
do_syscall_return(fd, cpu, ret, 0, 0, 0, 0); do_syscall_return(fd, cpu, ret, 0, 0, 0, 0);
break; break;
case __NR_clone:
__dprint("MIC clone(), new thread's cpu_id: %d\n", w.sr.args[0]);
do_syscall_return(fd, cpu, 0, 0, 0, 0, 0);
break;
case __NR_exit: case __NR_exit:
case __NR_exit_group: case __NR_exit_group:
do_syscall_return(fd, cpu, 0, 0, 0, 0, 0); do_syscall_return(fd, cpu, 0, 0, 0, 0, 0);