From 3341e7a3a184b5bea17f0f678b40bc51c92d72a4 Mon Sep 17 00:00:00 2001 From: Alex Auvolat Date: Mon, 9 Mar 2015 17:04:04 +0100 Subject: Partial implement VFS pager (enough for ISO9660!) Also add some debugging facilities (especially stack trace). --- src/kernel/user/ipc.c | 6 +-- src/kernel/user/nullfs.c | 14 ++--- src/kernel/user/pager.c | 129 +++++++++++++++++++++++++++++++++++++++------- src/kernel/user/process.c | 8 +-- src/kernel/user/vfs.c | 10 ++-- 5 files changed, 130 insertions(+), 37 deletions(-) (limited to 'src/kernel/user') diff --git a/src/kernel/user/ipc.c b/src/kernel/user/ipc.c index ff4d5c8..a8362ea 100644 --- a/src/kernel/user/ipc.c +++ b/src/kernel/user/ipc.c @@ -15,7 +15,7 @@ static int channel_poll(fs_handle_t *c, void** out_wait_obj); static bool channel_open(fs_node_ptr c, int mode); static bool channel_stat(fs_node_ptr c, stat_t *st); static void channel_close(fs_handle_t *c); -static void channel_dispose(fs_node_ptr c); +static void channel_dispose(fs_node_t *c); static fs_node_ops_t channel_ops = { .read = channel_read, @@ -214,8 +214,8 @@ void channel_close(fs_handle_t *ch) { // do nothing } -void channel_dispose(fs_node_ptr ch) { - channel_t *c = (channel_t*)ch; +void channel_dispose(fs_node_t *n) { + channel_t *c = (channel_t*)n->data; mutex_lock(&c->lock); diff --git a/src/kernel/user/nullfs.c b/src/kernel/user/nullfs.c index fa6a088..d4f345f 100644 --- a/src/kernel/user/nullfs.c +++ b/src/kernel/user/nullfs.c @@ -15,18 +15,18 @@ static void nullfs_fs_shutdown(fs_ptr fs); // nullfs directory node static bool nullfs_d_open(fs_node_ptr n, int mode); static bool nullfs_d_stat(fs_node_ptr n, stat_t *st); -static bool nullfs_d_walk(fs_node_ptr n, const char* file, struct fs_node *node_d); +static bool nullfs_d_walk(fs_node_t *n, const char* file, struct fs_node *node_d); static bool nullfs_d_delete(fs_node_ptr n, const char* file); static bool nullfs_d_move(fs_node_ptr n, const char* old_name, fs_node_t *new_parent, const char *new_name); static bool nullfs_d_create(fs_node_ptr n, const char* file, int type); -static void nullfs_d_dispose(fs_node_ptr n); +static void nullfs_d_dispose(fs_node_t *n); static bool nullfs_d_readdir(fs_handle_t *f, size_t ent_no, dirent_t *d); static void nullfs_d_close(fs_handle_t *f); // nullfs ram file node static bool nullfs_f_open(fs_node_ptr n, int mode); static bool nullfs_f_stat(fs_node_ptr n, stat_t *st); -static void nullfs_f_dispose(fs_node_ptr n); +static void nullfs_f_dispose(fs_node_t *n); static size_t nullfs_f_read(fs_handle_t *f, size_t offset, size_t len, char* buf); static size_t nullfs_f_write(fs_handle_t *f, size_t offset, size_t len, const char* buf); static void nullfs_f_close(fs_handle_t *f); @@ -250,8 +250,8 @@ bool nullfs_d_stat(fs_node_ptr n, stat_t *st) { return true; } -bool nullfs_d_walk(fs_node_ptr n, const char* file, struct fs_node *node_d) { - nullfs_dir_t* d = (nullfs_dir_t*)n; +bool nullfs_d_walk(fs_node_t *n, const char* file, struct fs_node *node_d) { + nullfs_dir_t* d = (nullfs_dir_t*)n->data; mutex_lock(&d->lock); @@ -415,7 +415,7 @@ bool nullfs_d_create(fs_node_ptr n, const char* file, int type) { } } -void nullfs_d_dispose(fs_node_ptr n) { +void nullfs_d_dispose(fs_node_t *n) { // nothing to do } @@ -486,7 +486,7 @@ bool nullfs_f_stat(fs_node_ptr n, stat_t *st) { return true; } -void nullfs_f_dispose(fs_node_ptr n) { +void nullfs_f_dispose(fs_node_t *n) { // nothing to do } diff --git a/src/kernel/user/pager.c b/src/kernel/user/pager.c index f03e45b..b1ce9c3 100644 --- a/src/kernel/user/pager.c +++ b/src/kernel/user/pager.c @@ -12,17 +12,17 @@ // SWAP PAGER // // ========== // -static void swap_page_in(pager_t *p, size_t offset, size_t len); -static void swap_page_out(pager_t *p, size_t offset, size_t len); -static void swap_page_release(pager_t *p, size_t offset, size_t len); -static bool swap_resize(pager_t *p, size_t new_size); +void swap_page_in(pager_t *p, size_t offset, size_t len); +void swap_page_out(pager_t *p, size_t offset, size_t len); +void swap_page_release(pager_t *p, size_t offset, size_t len); +bool swap_pager_resize(pager_t *p, size_t new_size); -static pager_ops_t swap_pager_ops = { +pager_ops_t swap_pager_ops = { .page_in = swap_page_in, .page_commit = 0, .page_out = swap_page_out, .page_release = swap_page_release, - .resize = swap_resize, + .resize = swap_pager_resize, }; pager_t *new_swap_pager(size_t size) { @@ -53,14 +53,15 @@ void swap_page_in(pager_t *p, size_t offset, size_t len) { for (size_t page = offset; page < offset + len; page += PAGE_SIZE) { if (hashtbl_find(p->pages, (void*)page) == 0) { uint32_t frame = frame_alloc(1); - if (frame != 0) { - if (!pd_map_page(region, frame, true)) PANIC("TODO");; - memset(region, 0, PAGE_SIZE); - pd_unmap_page(region); - } - if (!hashtbl_add(p->pages, (void*)page, (void*)(frame << ENT_FRAME_SHIFT))) { - frame_free(frame, 1); - } + + if (frame == 0) continue; + if (!pd_map_page(region, frame, true)) continue; + + memset(region, 0, PAGE_SIZE); + pd_unmap_page(region); + + bool ok = hashtbl_add(p->pages, (void*)page, (void*)(frame << ENT_FRAME_SHIFT)); + if (!ok) frame_free(frame, 1); } } @@ -83,7 +84,7 @@ void swap_page_release(pager_t *p, size_t offset, size_t len) { } } -bool swap_resize(pager_t *p, size_t new_size) { +bool swap_pager_resize(pager_t *p, size_t new_size) { // later : remove unused pages in swap file swap_page_release(p, PAGE_ALIGN_UP(new_size), p->size - PAGE_ALIGN_UP(new_size)); @@ -110,13 +111,105 @@ bool swap_resize(pager_t *p, size_t new_size) { return true; } +// ========= // +// VFS PAGER // +// ========= // + +void vfs_page_in(pager_t *p, size_t offset, size_t len); +void vfs_page_commit(pager_t *p, size_t offset, size_t len); +void vfs_page_out(pager_t *p, size_t offset, size_t len); +void vfs_page_release(pager_t *p, size_t offset, size_t len); +bool vfs_pager_resize(pager_t *p, size_t new_size); + +pager_ops_t vfs_pager_ops = { + .page_in = vfs_page_in, + .page_commit = vfs_page_commit, + .page_out = vfs_page_out, + .page_release = vfs_page_release, + .resize = vfs_pager_resize, +}; + +pager_t *new_vfs_pager(size_t size, fs_node_t *vfs_node, vfs_pager_ops_t *vfs_ops) { + pager_t *p = (pager_t*)malloc(sizeof(pager_t)); + if (p == 0) return 0; + + p->pages = create_hashtbl(id_key_eq_fun, id_hash_fun, 0); + if (p->pages == 0) goto error; + + p->ops = &vfs_pager_ops; + p->size = size; + p->lock = MUTEX_UNLOCKED; + p->maps = 0; + p->vfs_pager.node = vfs_node; + p->vfs_pager.ops = vfs_ops; + + return p; + +error: + if (p) free(p); + return 0; +} + +void vfs_page_in(pager_t *p, size_t offset, size_t len) { + ASSERT(PAGE_ALIGNED(offset)); + ASSERT(p->vfs_pager.ops->read != 0); + + void *region = region_alloc(PAGE_SIZE, "Page loading area", 0); + if (region == 0) return; + + for (size_t page = offset; page < offset + len; page += PAGE_SIZE) { + if (hashtbl_find(p->pages, (void*)page) == 0) { + bool ok = false; + uint32_t frame = frame_alloc(1); + + if (frame == 0) continue; + if (!pd_map_page(region, frame, true)) goto end; + + size_t expect_len = PAGE_SIZE; + if (page + expect_len > p->size) { + expect_len = p->size - page; + memset(region + expect_len, 0, PAGE_SIZE - expect_len); + } + + size_t read_len = p->vfs_pager.ops->read(p->vfs_pager.node, page, expect_len, (char*)region); + pd_unmap_page(region); + + if (read_len != expect_len) goto end; + + ok = hashtbl_add(p->pages, (void*)page, (void*)(frame << ENT_FRAME_SHIFT)); + + end: + if (!ok) frame_free(frame, 1); + } + } + + region_free(region); +} + +void vfs_page_commit(pager_t *p, size_t offset, size_t len) { + // TODO +} + +void vfs_page_out(pager_t *p, size_t offset, size_t len) { + // TODO +} + +void vfs_page_release(pager_t *p, size_t offset, size_t len) { + // TODO +} + +bool vfs_pager_resize(pager_t *p, size_t new_size) { + // TODO + return false; +} + // ============ // // DEVICE PAGER // // ============ // -static void device_page_in(pager_t *p, size_t offset, size_t len); +void device_page_in(pager_t *p, size_t offset, size_t len); -static pager_ops_t device_pager_ops = { +pager_ops_t device_pager_ops = { .page_in = device_page_in, .page_commit = 0, .page_out = 0, @@ -227,7 +320,7 @@ int pager_get_frame(pager_t *p, size_t offset) { return ret; } -static size_t pager_do_rw(pager_t *p, size_t offset, size_t len, char* buf, bool write) { +size_t pager_do_rw(pager_t *p, size_t offset, size_t len, char* buf, bool write) { size_t ret = 0; void* region = 0; diff --git a/src/kernel/user/process.c b/src/kernel/user/process.c index 7433517..d8f1eed 100644 --- a/src/kernel/user/process.c +++ b/src/kernel/user/process.c @@ -9,8 +9,8 @@ static int next_pid = 1; -static void proc_user_exception(registers_t *regs); -static void proc_usermem_pf(void* proc, registers_t *regs, void* addr); +void proc_user_exception(registers_t *regs); +void proc_usermem_pf(void* proc, registers_t *regs, void* addr); process_t *current_process() { if (current_thread) return current_thread->proc; @@ -661,12 +661,12 @@ void dbg_dump_proc_memmap(process_t *proc) { // USER MEMORY PAGE FAULT HANDLERS // // =============================== // -static void proc_user_exception(registers_t *regs) { +void proc_user_exception(registers_t *regs) { dbg_printf("Usermode exception in user process : exiting.\n"); dbg_dump_registers(regs); current_process_exit(PS_FAILURE, FAIL_EXCEPTION); } -static void proc_usermem_pf(void* p, registers_t *regs, void* addr) { +void proc_usermem_pf(void* p, registers_t *regs, void* addr) { process_t *proc = (process_t*)p; user_region_t *r = find_user_region(proc, addr); diff --git a/src/kernel/user/vfs.c b/src/kernel/user/vfs.c index 1fa1cc2..66ede94 100644 --- a/src/kernel/user/vfs.c +++ b/src/kernel/user/vfs.c @@ -101,7 +101,7 @@ void unref_fs(fs_t *fs) { unref_fs_node(fs->root); } else { ASSERT(fs->root->refs == 1); - if (fs->root->ops->dispose) fs->root->ops->dispose(fs->root->data); + if (fs->root->ops->dispose) fs->root->ops->dispose(fs->root); } if (fs->ops->shutdown) fs->ops->shutdown(fs->data); free(fs); @@ -130,7 +130,7 @@ void unref_fs_node(fs_node_t *n) { mutex_lock(&n->parent->lock); hashtbl_remove(n->parent->children, n->name); - if (n->ops->dispose) n->ops->dispose(n->data); + if (n->ops->dispose) n->ops->dispose(n); mutex_unlock(&n->parent->lock); unref_fs_node(n->parent); @@ -144,7 +144,7 @@ void unref_fs_node(fs_node_t *n) { // stand-alone IPC/SHM object ASSERT(n->parent == 0 && n->children == 0); - if (n->ops->dispose) n->ops->dispose(n->data); + if (n->ops->dispose) n->ops->dispose(n); } if (n->name) free(n->name); free(n); @@ -178,7 +178,7 @@ fs_node_t* fs_walk_one(fs_node_t* from, const char* file) { n->name = strdup(file); if (n->name == 0) goto error; - walk_ok = from->ops->walk && from->ops->walk(from->data, file, n); + walk_ok = from->ops->walk && from->ops->walk(from, file, n); if (!walk_ok) goto error; if (from->children == 0) { @@ -197,7 +197,7 @@ fs_node_t* fs_walk_one(fs_node_t* from, const char* file) { return n; error: - if (walk_ok) n->ops->dispose(n->data); + if (walk_ok) n->ops->dispose(n); if (n != 0 && n->name != 0) free(n->name); if (n != 0) free(n); mutex_unlock(&from->lock); -- cgit v1.2.3