static ke_handle memory_virtual_alloc(struct sysreq_memory_virtual_alloc *req) { unsigned long base, sz, type; ke_handle handle; struct ko_section *ks_virtual; if (req->name) { type = KS_TYPE_SHARE; TRACE_UNIMPLEMENTED("命名内存空间还没有支持"); goto err0; } else type = KS_TYPE_PRIVATE; base = req->base_address; sz = req->size; ks_virtual = ks_create(KP_CURRENT(), type, base, sz, req->mem_prot); if (!ks_virtual) goto err0; handle = ke_handle_create(ks_virtual); if (handle == KE_INVALID_HANDLE) goto err1; /* Fill output */ req->out_base = ks_virtual->node.start; req->out_size = ks_virtual->node.size; return handle; err1: TODO("分配内存段错误,须回收"); err0: return KE_INVALID_HANDLE; }
void *ke_map_file(void *fp, size_t map_size, page_prot_t prot) { struct ko_section *map; map = create_file_map(KP_CURRENT(), fp, map_size, prot); if (map) return (void*)map->node.start; return NULL; }
bool ke_handle_delete(ke_handle handle) { void * handle_table; struct ko_process * on = KP_CURRENT(); bool r; handle_table = get_handle_table(on); r = i2p_dealloc(handle_table, handle); put_handle_table(on); return r; }
void *ke_handle_translate(ke_handle handle) { void * object; void * handle_table; struct ko_process * on = KP_CURRENT(); handle_table = get_handle_table(on); object = i2p_find(handle_table, handle); if (object) cl_object_inc_ref(object); put_handle_table(on); return object; }
static ke_handle process_create(struct sysreq_process_create * req) { struct ko_process *process; struct ko_exe *ke_ld; ke_handle handle; if ((ke_ld = kp_exe_search_by_name("/os/i386/dl.sys")) == NULL) goto err1; if ((process = kp_run_user(ke_ld, req->cmdline)) == NULL) goto err2; if ((handle = ke_handle_create(KP_CURRENT())) == KE_INVALID_HANDLE) goto err3; kp_exe_put(ke_ld); return handle; err3: //TODO: delete the process err2: //TODO: close the object err1: return KE_INVALID_HANDLE; }
/** @brief Create a new user thread */ static ke_handle thread_create(struct sysreq_thread_create *req) { ke_handle h; struct ko_thread *t; struct kt_thread_creating_context ctx = {0}; ctx.thread_entry = req->entry; ctx.fate_entry = req->wrapper; ctx.para = req->para; ctx.flags = req->run?KT_CREATE_RUN:0; if ((t = kt_create(KP_CURRENT(), &ctx)) == NULL) goto err; if (KE_INVALID_HANDLE == (h = ke_handle_create((void*)t))) goto err; return h; err: if (t) { //TODO: } return KE_INVALID_HANDLE; }
ke_handle ke_handle_create(void *ko) { ke_handle h; void * handle_table; struct ko_process * on = KP_CURRENT(); handle_table = get_handle_table(on); /* Sanity */ if (handle_table == NULL) return KE_INVALID_HANDLE; h = i2p_alloc(handle_table, ko); if (h == COMMON_I2P_ALLOC_ERROR) { h = KE_INVALID_HANDLE; goto end; } cl_object_inc_ref(ko); end: put_handle_table(on); return h; }
/** @brief dynamic linker ops */ static ke_handle process_ld(struct sysreq_process_ld *req) { ke_handle handle; xstring module_name; switch (req->function_type) { case SYSREQ_PROCESS_OPEN_EXE: { struct ko_exe *image; module_name = req->name; if (ke_validate_user_buffer(req->context, req->context_length, true) == false) goto ld_0_err; if (ke_validate_user_buffer(module_name, strlen(module_name), false) == false) goto ld_0_err; if (ke_validate_user_buffer(&req->map_base, sizeof(req->map_base), true) == false) goto ld_0_err; if ((image = kp_exe_open_by_name(KP_CURRENT(), module_name, &req->map_base)) == NULL) goto ld_0_err; if (kp_exe_copy_private(image, req->context, req->context_length) == false) goto ld_0_err1; /* Create handle for this image */ handle = ke_handle_create(image); if (handle == KE_INVALID_HANDLE) goto ld_0_err1; return handle; ld_0_err1: //TODO: close the object ld_0_err: return KE_INVALID_HANDLE; } /* Return the map base */ case SYSREQ_PROCESS_MAP_EXE_FILE: { struct ko_section *file_section; module_name = req->name; //printk("mapping exe file %s at user.\n", module_name); if (ke_validate_user_buffer(module_name, strlen(module_name), false) == false) goto map_0_err; file_section = map_file(KP_CURRENT(), module_name, KM_PROT_READ, &req->context_length); if (file_section == NULL) goto map_0_err; /* We are preparing to use this section for EXE file ananlyzing, so no real handle is needed */ //printk("map ok\n"); return (ke_handle)file_section->node.start; map_0_err: return 0; } /* Unamp file or module in current process space, req->handle is the module handle(if not by address of req->name) req->name is map base, return bool */ case SYSREQ_PROCESS_UNMAP_EXE_FILE: { if (req->name) return unmap_file(KP_CURRENT(), req->name); //TODO handle } break; /* Add a new exe object, return bool */ case SYSREQ_PROCESS_ENJECT_EXE: { void *ctx; int ctx_size; struct ko_section *file_section; ctx_size = req->context_length; ctx = req->context; module_name = req->name; if (ke_validate_user_buffer(ctx, ctx_size, false) == false) goto ld_3_err; if (ke_validate_user_buffer(module_name, strlen(module_name), false) == false) goto ld_3_err; if (ctx_size > kp_exe_get_context_size()) goto ld_3_err; if ((file_section = map_file(kp_get_file_process(), module_name, KM_PROT_READ, NULL)) == NULL) goto ld_3_err; if (kp_exe_create_from_file(module_name, file_section, ctx, NULL) == NULL) goto ld_3_err; return true; ld_3_err: return false; } default: break; } return 0; }
/** @brief dynamic linker ops */ static ke_handle process_ld(struct sysreq_process_ld * req) { ke_handle handle; xstring module_name; switch (req->function_type) { case SYSREQ_PROCESS_OPEN_EXE: { struct ko_exe *image; module_name = req->name; if (ke_validate_user_buffer(req->context, req->context_length, true) == false) goto ld_0_err; if (ke_validate_user_buffer(module_name, strlen(module_name), false) == false) goto ld_0_err; if (ke_validate_user_buffer(&req->map_base, sizeof(req->map_base), true) == false) goto ld_0_err; if ((image = kp_exe_open_by_name(KP_CURRENT(), module_name, &req->map_base)) == NULL) goto ld_0_err; if (kp_exe_copy_private(image, req->context, req->context_length) == false) goto ld_0_err1; /* Create handle for this image */ handle = ke_handle_create(image); if (handle == KE_INVALID_HANDLE) goto ld_0_err1; return handle; ld_0_err1: //TODO: close the object ld_0_err: return KE_INVALID_HANDLE; } /* Return the map base */ case SYSREQ_PROCESS_MAP_EXE_FILE: { struct ko_section *file_section; module_name = req->name; if (ke_validate_user_buffer(module_name, strlen(module_name), false) == false) goto map_0_err; file_section = map_file(KP_CURRENT(), module_name, KM_PROT_READ, &req->context_length); if (file_section == NULL) goto map_0_err; return (ke_handle)file_section->node.start; map_0_err: return 0; } /* 删除本地map的文件 */ case SYSREQ_PROCESS_UNMAP_EXE_FILE: { void *base = req->name; TRACE_UNIMPLEMENTED(""); } break; /* Add a new exe object, return bool */ case SYSREQ_PROCESS_ENJECT_EXE: { struct ko_exe *image; void *ctx; int ctx_size; unsigned long size; struct ko_section *file_section; ctx_size = req->context_length; ctx = req->context; module_name = req->name; if (ke_validate_user_buffer(ctx, ctx_size, false) == false) goto ld_3_err; if (ke_validate_user_buffer(module_name, strlen(module_name), false) == false) goto ld_3_err; if (ctx_size > kp_exe_get_context_size()) goto ld_3_err; if ((file_section = map_file(kp_get_file_process(), module_name, KM_PROT_READ, &size)) == NULL) goto ld_3_err; if (kp_exe_create_from_file(module_name, file_section, ctx, NULL) == NULL) goto ld_3_err; return true; ld_3_err: return false; } default: break; } return 0; }