feat: a file system mapping for pci devices
authorMinep <zelong56@gmail.com>
Thu, 25 Aug 2022 15:04:33 +0000 (16:04 +0100)
committerMinep <zelong56@gmail.com>
Thu, 25 Aug 2022 15:04:33 +0000 (16:04 +0100)
chore: some fixes around and cleanup.

17 files changed:
lunaix-os/hal/ahci/ahci.c
lunaix-os/hal/pci.c
lunaix-os/includes/hal/pci.h
lunaix-os/includes/lunaix/fs.h
lunaix-os/includes/lunaix/fs/twifs.h
lunaix-os/includes/lunaix/status.h
lunaix-os/kernel/block.c
lunaix-os/kernel/demos/simple_sh.c
lunaix-os/kernel/fs/path_walk.c
lunaix-os/kernel/fs/pcache.c
lunaix-os/kernel/fs/twifs/twifs.c
lunaix-os/kernel/fs/vfs.c
lunaix-os/kernel/mm/vmap.c
lunaix-os/kernel/proc0.c
lunaix-os/kernel/tty/lxconsole.c
lunaix-os/kernel/tty/tty.c
lunaix-os/libs/klibc/string/strcpy.c

index 9a7073a6c0c17c58363eb8091434e37d15d96d26..206112e1d79d0e99c85ab8bc0144c4422f99c4db 100644 (file)
@@ -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
index 3d6accfafaacf31dc9d026c44b4a60e471a11191..ca31e21c7656d05eddd6a9931429a588c3fe0cb4 100644 (file)
@@ -11,6 +11,7 @@
 #include <hal/acpi/acpi.h>
 #include <hal/apic.h>
 #include <hal/pci.h>
+#include <lunaix/fs/twifs.h>
 #include <lunaix/mm/valloc.h>
 #include <lunaix/spike.h>
 #include <lunaix/syslog.h>
@@ -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
index 57c04ca6d4661a9b19cc87cf754065482532a5ec..24c4e2963b804ab0497ca0a0f95f6498a3ad96fd 100644 (file)
@@ -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.
index 099d47ed94378936e164f02f7566a22b80a66eba..b497954abce0373140bc97cf4f800dfab89f8561 100644 (file)
@@ -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,
index f4614d8f72d4fe4e9f3a1a42664f059f1ed4cabc..563411223d8d5d258110e73b26e7d70024888500 100644 (file)
@@ -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);
index 0e381b48a63d5e54dbca6abfa3f87fb5a81c3365..cc105b469ccede5f2ff622a155c0c24a7b463232 100644 (file)
@@ -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
index b855910ffc18fc12a1506e105540dda74fc62964..3d3f97c860c4adc90a34ac2efbec609d0bb2962b 100644 (file)
@@ -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;
index 8243ae1d85352141c36274a0462942a1b1354c0d..4ea9711a925438232e4fbc14386f13ee3b6c6554 100644 (file)
@@ -8,6 +8,7 @@
 #include <ulibc/stdio.h>
 
 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");
         }
index c7d4ecb1e23f4a0914332928941515f8db8242b5..c80d7134d22596d89a7f53addcfb59929c3da8a5 100644 (file)
@@ -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;
         }
index 0094ec4bc0c7fde1e67f2a799e180ff302f67c1e..c27f78ba6bf8282139c0f487eb761acb4e456f9d 100644 (file)
@@ -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;
             }
index d452d0b876606363388884d3fdac96413cbfdb09..9a5dd8e9fd4f3370efed1a684b3153e1aa355ae6 100644 (file)
@@ -8,12 +8,14 @@
  * @copyright Copyright (c) 2022
  *
  */
+#include <klibc/stdio.h>
 #include <klibc/string.h>
 #include <lunaix/clock.h>
 #include <lunaix/fs.h>
 #include <lunaix/fs/twifs.h>
 #include <lunaix/mm/cake.h>
 #include <lunaix/mm/valloc.h>
+#include <lunaix/spike.h>
 
 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
index 0c2f42d8459a0f440f488dfb1dd8dc14c63294b3..eaf35e03087e963dea8c5509dc543f8c43b40798 100644 (file)
@@ -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;
index 2fc129841513dfcf3dbe0431e4068bc3c27bdfbc..7507b1a86113a5364d6e6020d6e5a4724ca239a7 100644 (file)
@@ -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;
         }
     }
index cd7681854e86dfefca8455928256a7aefd9c5614..bdc23ced68618e2d89f95096b81c9471f752ae84 100644 (file)
@@ -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
index 92deb498d1dd3faa6fbf1786adbd298bf2b13bf4..a226e4221e8253336eb477c97ef114597d4c0460 100644 (file)
@@ -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);
index 6db2417a7d81c5524e2c66f0c3460c3aeed4ab11..f197559e8b6a41607180bdea361c1e83dcfcbba1 100644 (file)
@@ -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);
index 12586064cd6767fe36abb89139dc4eb2f180a45a..1f7a06d08a928333909ec35d917eeaf08382c42b 100644 (file)
@@ -1,11 +1,11 @@
 #include <klibc/string.h>
 
 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