From bb7ce16533fb6c1384775dea6e1150e74c229daf Mon Sep 17 00:00:00 2001 From: Minep Date: Thu, 25 Aug 2022 16:04:33 +0100 Subject: [PATCH 1/1] feat: a file system mapping for pci devices chore: some fixes around and cleanup. --- lunaix-os/hal/ahci/ahci.c | 2 +- lunaix-os/hal/pci.c | 66 ++++++++++++++-------------- lunaix-os/includes/hal/pci.h | 3 -- lunaix-os/includes/lunaix/fs.h | 4 +- lunaix-os/includes/lunaix/fs/twifs.h | 14 ++---- lunaix-os/includes/lunaix/status.h | 2 +- lunaix-os/kernel/block.c | 11 ++--- lunaix-os/kernel/demos/simple_sh.c | 20 +++++++++ lunaix-os/kernel/fs/path_walk.c | 4 +- lunaix-os/kernel/fs/pcache.c | 4 +- lunaix-os/kernel/fs/twifs/twifs.c | 61 +++++++++++++------------ lunaix-os/kernel/fs/vfs.c | 12 +++-- lunaix-os/kernel/mm/vmap.c | 9 ++-- lunaix-os/kernel/proc0.c | 4 ++ lunaix-os/kernel/tty/lxconsole.c | 10 +++-- lunaix-os/kernel/tty/tty.c | 9 ++-- lunaix-os/libs/klibc/string/strcpy.c | 15 ++++--- 17 files changed, 131 insertions(+), 119 deletions(-) diff --git a/lunaix-os/hal/ahci/ahci.c b/lunaix-os/hal/ahci/ahci.c index 9a7073a..206112e 100644 --- a/lunaix-os/hal/ahci/ahci.c +++ b/lunaix-os/hal/ahci/ahci.c @@ -194,7 +194,7 @@ __ahci_hba_isr(const isr_param* param) { // TODO: clear the interrupt status // TODO: I/O-operation scheduler should be here - kprintf(KDEBUG "HBA INTR\n"); + // kprintf(KDEBUG "HBA INTR\n"); } void diff --git a/lunaix-os/hal/pci.c b/lunaix-os/hal/pci.c index 3d6accf..ca31e21 100644 --- a/lunaix-os/hal/pci.c +++ b/lunaix-os/hal/pci.c @@ -11,6 +11,7 @@ #include #include #include +#include #include #include #include @@ -108,45 +109,40 @@ pci_probe_msi_info(struct pci_device* device) #define PCI_PRINT_BAR_LISTING +int +__pci_read_cspace(struct v_inode* inode, void* buffer, size_t len, size_t fpos) +{ + if (len < 256) { + return ERANGE; + } + + struct twifs_node* node = (struct twifs_node*)(inode->data); + struct pci_device* pcidev = (struct pci_device*)(node->data); + + for (size_t i = 0; i < 256; i += sizeof(pci_reg_t)) { + *(pci_reg_t*)(buffer + i) = pci_read_cspace(pcidev->cspace_base, i); + } + + return 256; +} + void -pci_print_device() +pci_build_fsmapping() { + struct twifs_node *pci_class = twifs_dir_node(NULL, "pci"), *pci_dev; struct pci_device *pos, *n; llist_for_each(pos, n, &pci_devices, dev_chain) { - kprintf(KINFO "(B%xh:D%xh:F%xh) Dev %x:%x, Class 0x%x\n", - PCI_BUS_NUM(pos->cspace_base), - PCI_SLOT_NUM(pos->cspace_base), - PCI_FUNCT_NUM(pos->cspace_base), - PCI_DEV_VENDOR(pos->device_info), - PCI_DEV_DEVID(pos->device_info), - PCI_DEV_CLASS(pos->class_info)); - - kprintf(KINFO "\t IRQ: %d, INT#x: %d\n", - PCI_INTR_IRQ(pos->intr_info), - PCI_INTR_PIN(pos->intr_info)); -#ifdef PCI_PRINT_BAR_LISTING - uint32_t bar; - for (size_t i = 1; i <= 6; i++) { - size_t size = pci_bar_sizing(pos, &bar, i); - if (!bar) - continue; - if (PCI_BAR_MMIO(bar)) { - kprintf(KINFO "\t BAR#%d (MMIO) %p [%d]\n", - i, - PCI_BAR_ADDR_MM(bar), - size); - } else { - kprintf(KINFO "\t BAR#%d (I/O) %p [%d]\n", - i, - PCI_BAR_ADDR_IO(bar), - size); - } - } -#endif - if (pos->msi_loc) { - kprintf(KINFO "\t MSI supported (@%xh)\n", pos->msi_loc); - } + pci_dev = twifs_dir_node(pci_class, + "B%d:D%d:F%d.%x:%x", + PCI_BUS_NUM(pos->cspace_base), + PCI_SLOT_NUM(pos->cspace_base), + PCI_FUNCT_NUM(pos->cspace_base), + PCI_DEV_VENDOR(pos->device_info), + PCI_DEV_DEVID(pos->device_info)); + struct twifs_node* fnode = twifs_file_node(pci_dev, "cspace"); + fnode->data = pos; + fnode->ops.read = __pci_read_cspace; } } @@ -241,4 +237,6 @@ pci_init() } // Otherwise, fallback to use legacy PCI 3.0 method. pci_probe(); + + pci_build_fsmapping(); } \ No newline at end of file diff --git a/lunaix-os/includes/hal/pci.h b/lunaix-os/includes/hal/pci.h index 57c04ca..24c4e29 100644 --- a/lunaix-os/includes/hal/pci.h +++ b/lunaix-os/includes/hal/pci.h @@ -91,9 +91,6 @@ pci_write_cspace(uint32_t base, int offset, pci_reg_t data) void pci_init(); -void -pci_print_device(); - /** * @brief 根据类型代码(Class Code)去在拓扑中寻找一个设备 * 类型代码请参阅: PCI LB Spec. Appendix D. diff --git a/lunaix-os/includes/lunaix/fs.h b/lunaix-os/includes/lunaix/fs.h index 099d47e..b497954 100644 --- a/lunaix-os/includes/lunaix/fs.h +++ b/lunaix-os/includes/lunaix/fs.h @@ -44,7 +44,7 @@ #define VFS_VALID_CHAR(chr) \ (('A' <= (chr) && (chr) <= 'Z') || ('a' <= (chr) && (chr) <= 'z') || \ ('0' <= (chr) && (chr) <= '9') || (chr) == '.' || (chr) == '_' || \ - (chr) == '-') + (chr) == '-' || (chr) == ':') #define unlock_inode(inode) mutex_unlock(&inode->lock) #define lock_inode(inode) \ @@ -369,7 +369,7 @@ pcache_new_page(struct pcache* pcache, uint32_t index); void pcache_set_dirty(struct pcache* pcache, struct pcache_pg* pg); -struct pcache_pg* +int pcache_get_page(struct pcache* pcache, uint32_t index, uint32_t* offset, diff --git a/lunaix-os/includes/lunaix/fs/twifs.h b/lunaix-os/includes/lunaix/fs/twifs.h index f4614d8..5634112 100644 --- a/lunaix-os/includes/lunaix/fs/twifs.h +++ b/lunaix-os/includes/lunaix/fs/twifs.h @@ -9,6 +9,7 @@ struct twifs_node inode_t ino_id; void* data; uint32_t itype; + char name_val[VFS_NAME_MAXLEN]; struct llist_header children; struct llist_header siblings; struct @@ -28,19 +29,10 @@ void twifs_init(); struct twifs_node* -twifs_file_node(struct twifs_node* parent, - const char* name, - int name_len, - uint32_t itype); +twifs_file_node(struct twifs_node* parent, const char* fmt, ...); struct twifs_node* -twifs_dir_node(struct twifs_node* parent, - const char* name, - int name_len, - uint32_t itype); - -struct twifs_node* -twifs_toplevel_node(const char* name, int name_len, uint32_t itype); +twifs_dir_node(struct twifs_node* parent, const char* fmt, ...); int twifs_rm_node(struct twifs_node* node); diff --git a/lunaix-os/includes/lunaix/status.h b/lunaix-os/includes/lunaix/status.h index 0e381b4..cc105b4 100644 --- a/lunaix-os/includes/lunaix/status.h +++ b/lunaix-os/includes/lunaix/status.h @@ -17,7 +17,7 @@ #define EEXIST -12 #define EBADF -13 #define ENOTSUP -14 -#define ENXIO -15 +#define EIO -15 #define ELOOP -16 #define ENOTEMPTY -17 #define EROFS -18 diff --git a/lunaix-os/kernel/block.c b/lunaix-os/kernel/block.c index b855910..3d3f97c 100644 --- a/lunaix-os/kernel/block.c +++ b/lunaix-os/kernel/block.c @@ -44,13 +44,14 @@ __block_read(struct device* dev, void* buf, size_t offset, size_t len) int errno; struct block_dev* bdev = (struct block_dev*)dev->underlay; size_t acc_size = 0, rd_size = 0, bsize = bdev->hd_dev->block_size, - rd_block = offset / bsize, r = offset % bsize; + rd_block = offset / bsize, r = offset % bsize, + max_blk = (size_t)bdev->hd_dev->max_lba; void* block = vzalloc(bsize); - while (acc_size < len) { + while (acc_size < len && rd_block < max_blk) { if (!bdev->hd_dev->ops.read_buffer( bdev->hd_dev, rd_block, block, bsize)) { - errno = ENXIO; + errno = EIO; goto error; } rd_size = MIN(len - acc_size, bsize - r); @@ -61,7 +62,7 @@ __block_read(struct device* dev, void* buf, size_t offset, size_t len) } vfree(block); - return rd_block; + return acc_size; error: vfree(block); @@ -82,7 +83,7 @@ __block_write(struct device* dev, void* buf, size_t offset, size_t len) memcpy(block + r, buf + acc_size, wr_size); if (!bdev->hd_dev->ops.write_buffer( bdev->hd_dev, wr_block, block, bsize)) { - errno = ENXIO; + errno = EIO; break; } acc_size += wr_size; diff --git a/lunaix-os/kernel/demos/simple_sh.c b/lunaix-os/kernel/demos/simple_sh.c index 8243ae1..4ea9711 100644 --- a/lunaix-os/kernel/demos/simple_sh.c +++ b/lunaix-os/kernel/demos/simple_sh.c @@ -8,6 +8,7 @@ #include char pwd[512]; +char cat_buf[1024]; /* Simple shell - (actually this is not even a shell) @@ -55,6 +56,9 @@ sh_printerr() case ENOMEM: printf("Error: Out of memory\n"); break; + case EISDIR: + printf("Error: This is a directory\n"); + break; default: printf("Error: Fail to open (%d)\n", errno); break; @@ -102,6 +106,22 @@ sh_main() close(fd); } + } else if (streq(cmd, "cat")) { + int fd = open(argpart, 0); + if (fd < 0) { + sh_printerr(); + } else { + int sz; + while ((sz = read(fd, cat_buf, 1024)) == 1024) { + write(stdout, cat_buf, 1024); + } + if (sz < 0) { + sh_printerr(); + } else { + write(stdout, cat_buf, sz); + } + close(fd); + } } else { printf("unknow command"); } diff --git a/lunaix-os/kernel/fs/path_walk.c b/lunaix-os/kernel/fs/path_walk.c index c7d4ecb..c80d713 100644 --- a/lunaix-os/kernel/fs/path_walk.c +++ b/lunaix-os/kernel/fs/path_walk.c @@ -74,9 +74,7 @@ __vfs_walk(struct v_dnode* start, if (!lookahead && (walk_options & VFS_WALK_PARENT)) { if (component) { - component->hash = name.hash; - component->len = j; - strcpy(component->value, fname_buffer); + hstrcpy(component, &name); } break; } diff --git a/lunaix-os/kernel/fs/pcache.c b/lunaix-os/kernel/fs/pcache.c index 0094ec4..c27f78b 100644 --- a/lunaix-os/kernel/fs/pcache.c +++ b/lunaix-os/kernel/fs/pcache.c @@ -76,7 +76,7 @@ pcache_set_dirty(struct pcache* pcache, struct pcache_pg* pg) } } -struct pcache_pg* +int pcache_get_page(struct pcache* pcache, uint32_t index, uint32_t* offset, @@ -140,7 +140,7 @@ pcache_read(struct v_inode* inode, void* data, uint32_t len, uint32_t fpos) errno = inode->default_fops->read(inode, pg->pg, PG_SIZE, pg->fpos); if (errno >= 0 && errno < PG_SIZE) { // EOF - len = buf_off + errno; + len = MIN(len, buf_off + errno); } else if (errno < 0) { break; } diff --git a/lunaix-os/kernel/fs/twifs/twifs.c b/lunaix-os/kernel/fs/twifs/twifs.c index d452d0b..9a5dd8e 100644 --- a/lunaix-os/kernel/fs/twifs/twifs.c +++ b/lunaix-os/kernel/fs/twifs/twifs.c @@ -8,12 +8,14 @@ * @copyright Copyright (c) 2022 * */ +#include #include #include #include #include #include #include +#include static struct twifs_node* fs_root; @@ -33,7 +35,9 @@ __twifs_new_node(struct twifs_node* parent, struct twifs_node* node = cake_grab(twi_pile); memset(node, 0, sizeof(*node)); - node->name = HSTR(name, name_len); + strncpy(node->name_val, name, VFS_NAME_MAXLEN); + + node->name = HSTR(node->name_val, MIN(name_len, VFS_NAME_MAXLEN)); node->itype = itype; node->ino_id = inode_id++; hstr_rehash(&node->name, HSTR_FULL_HASH); @@ -120,24 +124,24 @@ __twifs_dirlookup(struct v_inode* inode, struct v_dnode* dnode) struct twifs_node* child_node = __twifs_get_node(twi_node, &dnode->name); if (child_node) { - struct v_inode* inode = vfs_i_find(inode->sb, child_node->ino_id); - if (inode) { + struct v_inode* child_inode = vfs_i_find(inode->sb, child_node->ino_id); + if (child_inode) { goto done; } - if (!(inode = vfs_i_alloc(inode->sb))) { + if (!(child_inode = vfs_i_alloc(inode->sb))) { return ENOENT; } - inode->id = child_node->ino_id; - inode->itype = child_node->itype; - inode->data = child_node; + child_inode->id = child_node->ino_id; + child_inode->itype = child_node->itype; + child_inode->data = child_node; - vfs_i_addhash(inode); + vfs_i_addhash(child_inode); done: dnode->data = child_node->data; - vfs_assign_inode(dnode, inode); + vfs_assign_inode(dnode, child_inode); return 0; } return ENOENT; @@ -185,40 +189,35 @@ twifs_rm_node(struct twifs_node* node) } struct twifs_node* -twifs_file_node(struct twifs_node* parent, - const char* name, - int name_len, - uint32_t itype) +twifs_file_node(struct twifs_node* parent, const char* fmt, ...) { + va_list args; + va_start(args, fmt); + + char buf[VFS_NAME_MAXLEN]; + size_t len = __ksprintf_internal(buf, fmt, VFS_NAME_MAXLEN, args); struct twifs_node* twi_node = - __twifs_new_node(parent, name, name_len, VFS_IFFILE | itype); + __twifs_new_node(parent ? parent : fs_root, buf, len, VFS_IFSEQDEV); + + va_end(args); return twi_node; } struct twifs_node* -twifs_dir_node(struct twifs_node* parent, - const char* name, - int name_len, - uint32_t itype) +twifs_dir_node(struct twifs_node* parent, const char* fmt, ...) { - struct hstr hname = HSTR(name, name_len); - hstr_rehash(&hname, HSTR_FULL_HASH); - struct twifs_node* node = __twifs_get_node(parent, &hname); - if (node) { - return node; - } + va_list args; + va_start(args, fmt); + char buf[VFS_NAME_MAXLEN]; + size_t len = __ksprintf_internal(buf, fmt, VFS_NAME_MAXLEN, args); struct twifs_node* twi_node = - __twifs_new_node(parent, name, name_len, VFS_IFDIR | itype); + __twifs_new_node(parent ? parent : fs_root, buf, len, VFS_IFDIR); - return twi_node; -} + va_end(args); -struct twifs_node* -twifs_toplevel_node(const char* name, int name_len, uint32_t itype) -{ - return twifs_dir_node(fs_root, name, name_len, itype); + return twi_node; } void diff --git a/lunaix-os/kernel/fs/vfs.c b/lunaix-os/kernel/fs/vfs.c index 0c2f42d..eaf35e0 100644 --- a/lunaix-os/kernel/fs/vfs.c +++ b/lunaix-os/kernel/fs/vfs.c @@ -646,13 +646,11 @@ __DEFINE_LXSYSCALL3(int, read, int, fd, void*, buf, size_t, count) file->inode->atime = clock_unixtime(); - __SYSCALL_INTERRUPTIBLE({ - if ((file->inode->itype & VFS_IFSEQDEV) || (fd_s->flags & FO_DIRECT)) { - errno = file->ops->read(file->inode, buf, count, file->f_pos); - } else { - errno = pcache_read(file->inode, buf, count, file->f_pos); - } - }) + if ((file->inode->itype & VFS_IFSEQDEV) || (fd_s->flags & FO_DIRECT)) { + errno = file->ops->read(file->inode, buf, count, file->f_pos); + } else { + errno = pcache_read(file->inode, buf, count, file->f_pos); + } if (errno > 0) { file->f_pos += errno; diff --git a/lunaix-os/kernel/mm/vmap.c b/lunaix-os/kernel/mm/vmap.c index 2fc1298..7507b1a 100644 --- a/lunaix-os/kernel/mm/vmap.c +++ b/lunaix-os/kernel/mm/vmap.c @@ -26,18 +26,18 @@ vmm_vmap(uintptr_t paddr, size_t size, pt_attr attr) current_addr = (current_addr & 0xffc00000) + MEM_4MB; } else { x86_page_table* ptd = (x86_page_table*)(L2_VADDR(l1inx)); - size_t i = L2_INDEX(current_addr); - for (; i < PG_MAX_ENTRIES && examed_size < size; i++) { + size_t i = L2_INDEX(current_addr), j = 0; + for (; i < PG_MAX_ENTRIES && examed_size < size; i++, j++) { if (!ptd->entry[i]) { examed_size += PG_SIZE; } else if (examed_size) { // found a discontinuity, start from beginning examed_size = 0; - i++; + j++; break; } } - current_addr += i << 12; + current_addr += j << 12; } if (examed_size >= size) { @@ -46,6 +46,7 @@ vmm_vmap(uintptr_t paddr, size_t size, pt_attr attr) if (current_addr >= VMAP_END) { wrapped = 1; + examed_size = 0; current_addr = VMAP_START; } } diff --git a/lunaix-os/kernel/proc0.c b/lunaix-os/kernel/proc0.c index cd76818..bdc23ce 100644 --- a/lunaix-os/kernel/proc0.c +++ b/lunaix-os/kernel/proc0.c @@ -179,6 +179,10 @@ init_platform() vmm_del_mapping(PD_REFERENCED, (void*)i); pmm_free_page(KERNEL_PID, (void*)i); } + + for (size_t i = L1_INDEX(KERNEL_MM_BASE); i < 1023; i++) { + vmm_set_mapping(PD_REFERENCED, i << 22, 0, 0, VMAP_NOMAP); + } } void diff --git a/lunaix-os/kernel/tty/lxconsole.c b/lunaix-os/kernel/tty/lxconsole.c index 92deb49..a226e42 100644 --- a/lunaix-os/kernel/tty/lxconsole.c +++ b/lunaix-os/kernel/tty/lxconsole.c @@ -186,7 +186,6 @@ console_write(struct console* console, uint8_t* data, size_t size) uintptr_t rd_ptr = fbuf->rd_pos; char c; - int j = 0; for (size_t i = 0; i < size; i++) { c = data[i]; if (!c) { @@ -194,12 +193,15 @@ console_write(struct console* console, uint8_t* data, size_t size) } if (c == '\n') { console->lines++; + } else if (c == '\x08') { + ptr = ptr ? ptr - 1 : fbuf->size - 1; + continue; } - buffer[(ptr + j) % fbuf->size] = c; - j++; + buffer[ptr] = c; + ptr = (ptr + 1) % fbuf->size; } - fifo_set_wrptr(fbuf, (ptr + j) % fbuf->size); + fifo_set_wrptr(fbuf, ptr); while (console->lines >= TTY_HEIGHT) { rd_ptr = __find_next_line(rd_ptr); diff --git a/lunaix-os/kernel/tty/tty.c b/lunaix-os/kernel/tty/tty.c index 6db2417..f197559 100644 --- a/lunaix-os/kernel/tty/tty.c +++ b/lunaix-os/kernel/tty/tty.c @@ -83,11 +83,10 @@ tty_flush_buffer(struct fifo_buf* buf) case '\r': x = 0; break; - case '\x08': - x = x ? x - 1 : 0; - *(tty_vga_buffer + x + y * TTY_WIDTH) = - (current_theme | 0x20); - break; + // case '\x08': + // *(tty_vga_buffer + x + y * TTY_WIDTH) = + // (current_theme | 0x20); + // break; default: *(tty_vga_buffer + x + y * TTY_WIDTH) = (current_theme | chr); diff --git a/lunaix-os/libs/klibc/string/strcpy.c b/lunaix-os/libs/klibc/string/strcpy.c index 1258606..1f7a06d 100644 --- a/lunaix-os/libs/klibc/string/strcpy.c +++ b/lunaix-os/libs/klibc/string/strcpy.c @@ -1,11 +1,11 @@ #include char* -strcpy(char* dest, const char* src) { +strcpy(char* dest, const char* src) +{ char c; unsigned int i = 0; - while ((c = src[i])) - { + while ((c = src[i])) { dest[i] = c; i++; } @@ -14,10 +14,13 @@ strcpy(char* dest, const char* src) { } char* -strncpy(char* dest, const char* src, size_t n) { +strncpy(char* dest, const char* src, size_t n) +{ char c; unsigned int i = 0; - while ((c = src[i]) && i < n) dest[i++] = c; - while (i < n) dest[i++] = 0; + while ((c = src[i]) && i <= n) + dest[i++] = c; + while (i <= n) + dest[i++] = 0; return dest; } \ No newline at end of file -- 2.27.0