Skip to content

Instantly share code, notes, and snippets.

@mopp
Last active August 29, 2015 14:10
Show Gist options
  • Save mopp/d5c1f38ac1690529aeaa to your computer and use it in GitHub Desktop.
Save mopp/d5c1f38ac1690529aeaa to your computer and use it in GitHub Desktop.
static inline Axel_state_code init_user_process(void) {
// プロセス構造体を新規確保
Process* p = alloc_proc(NULL);
User_segments* us = &p->u_segs;
// プログラム用のメモリ確保
us->text.addr = DEFAULT_TEXT_ADDR;
us->text.size = 0;
us->stack.addr = DEFAULT_STACK_TOP_ADDR;
us->stack.size = 0;
expand_segment(p, &us->text, DEFAULT_TEXT_SIZE);
expand_segment(p, &us->stack, DEFAULT_STACK_SIZE);
// 実行を再開アドレスをinterrupt_returnに設定
p->thread.ip = (uintptr_t)interrupt_return;
// カーネルモードスタックを操作し、割り込まれた時のように設定
p->thread.sp = p->km_stack;
p->thread.sp -= sizeof(Interrupt_frame);
Interrupt_frame* intf = (Interrupt_frame*)p->thread.sp;
p->thread.iframe = intf;
memset(intf, 0, sizeof(Interrupt_frame));
intf->ds = USER_DATA_SEGMENT_SELECTOR;
intf->es = USER_DATA_SEGMENT_SELECTOR;
intf->fs = USER_DATA_SEGMENT_SELECTOR;
intf->gs = USER_DATA_SEGMENT_SELECTOR;
intf->eip = ECAST_UINT32(us->text.addr);
intf->cs = USER_CODE_SEGMENT_SELECTOR;
intf->eflags = 0x00000200;
intf->prev_esp = ECAST_UINT32(us->stack.addr + DEFAULT_STACK_SIZE);
intf->prev_ss = USER_DATA_SEGMENT_SELECTOR;
/* 実行プログラムをディスクから読み込み */
File* f = resolve_path(axel_s.fs, "init");
void* fbuf = kmalloc(f->size);
if (f->belong_fs->access_file(FILE_READ, f, fbuf) != AXEL_SUCCESS ) {
kfree(fbuf);
return AXEL_FAILED;
}
set_cpu_pdt(get_page_phys_addr(&p->pdt_page));
memcpy((void*)us->text.addr, fbuf, f->size);
set_cpu_pdt(vir_to_phys_addr((uintptr_t)(get_kernel_pdt())));
kfree(fbuf);
// プロセスの設定が終わったので実行可能状態へ
p->state = PROC_STATE_RUN;
return AXEL_SUCCESS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment