From: Minep Date: Wed, 20 Jul 2022 12:24:25 +0000 (+0100) Subject: refactor: change the disk io api to accept device instead of port struct X-Git-Url: https://scm.lunaixsky.com/lunaix-os.git/commitdiff_plain/489fcbc119f6ae46efc8f4077cbb5aa43683e404?hp=e8e64a4f1d76aaeac3defa13243505cccd25c078 refactor: change the disk io api to accept device instead of port struct refactor: add vzalloc and make vcalloc sounds right. chore: format code and renaming things --- diff --git a/lunaix-os/hal/ahci/ahci.c b/lunaix-os/hal/ahci/ahci.c index 133513b..1562676 100644 --- a/lunaix-os/hal/ahci/ahci.c +++ b/lunaix-os/hal/ahci/ahci.c @@ -316,7 +316,8 @@ ahci_init_device(struct hba_port* port) // 清空任何待响应的中断 port->regs[HBA_RPxIS] = 0; - port->device = vcalloc(sizeof(struct hba_device)); + port->device = vzalloc(sizeof(struct hba_device)); + port->device->port = port; // 在命令表中构建命令FIS struct sata_reg_fis* cmd_fis = (struct sata_reg_fis*)cmd_table->command_fis; diff --git a/lunaix-os/hal/ahci/ata.c b/lunaix-os/hal/ahci/ata.c index f6ff7c1..a2f10ba 100644 --- a/lunaix-os/hal/ahci/ata.c +++ b/lunaix-os/hal/ahci/ata.c @@ -6,7 +6,7 @@ #include int -__sata_buffer_io(struct hba_port* port, +__sata_buffer_io(struct hba_device* dev, uint64_t lba, void* buffer, uint32_t size, @@ -14,6 +14,7 @@ __sata_buffer_io(struct hba_port* port, { assert_msg(((uintptr_t)buffer & 0x3) == 0, "HBA: Bad buffer alignment"); + struct hba_port* port = dev->port; struct hba_cmdh* header; struct hba_cmdt* table; int slot = hba_prepare_cmd(port, &table, &header, buffer, size); @@ -70,21 +71,21 @@ fail: } int -sata_read_buffer(struct hba_port* port, +sata_read_buffer(struct hba_device* dev, uint64_t lba, void* buffer, uint32_t size) { - return __sata_buffer_io(port, lba, buffer, size, 0); + return __sata_buffer_io(dev, lba, buffer, size, 0); } int -sata_write_buffer(struct hba_port* port, +sata_write_buffer(struct hba_device* dev, uint64_t lba, void* buffer, uint32_t size) { - return __sata_buffer_io(port, lba, buffer, size, 1); + return __sata_buffer_io(dev, lba, buffer, size, 1); } void diff --git a/lunaix-os/hal/ahci/atapi.c b/lunaix-os/hal/ahci/atapi.c index 397c733..0ef5099 100644 --- a/lunaix-os/hal/ahci/atapi.c +++ b/lunaix-os/hal/ahci/atapi.c @@ -42,7 +42,7 @@ scsi_parse_capacity(struct hba_device* device, uint32_t* parameter) } void -__scsi_buffer_io(struct hba_port* port, +__scsi_buffer_io(struct hba_device* dev, uint64_t lba, void* buffer, uint32_t size, @@ -50,6 +50,7 @@ __scsi_buffer_io(struct hba_port* port, { assert_msg(((uintptr_t)buffer & 0x3) == 0, "HBA: Bad buffer alignment"); + struct hba_port* port = dev->port; struct hba_cmdh* header; struct hba_cmdt* table; int slot = hba_prepare_cmd(port, &table, &header, buffer, size); @@ -107,19 +108,19 @@ fail: } void -scsi_read_buffer(struct hba_port* port, +scsi_read_buffer(struct hba_device* dev, uint64_t lba, void* buffer, uint32_t size) { - __scsi_buffer_io(port, lba, buffer, size, 0); + __scsi_buffer_io(dev, lba, buffer, size, 0); } void -scsi_write_buffer(struct hba_port* port, +scsi_write_buffer(struct hba_device* dev, uint64_t lba, void* buffer, uint32_t size) { - __scsi_buffer_io(port, lba, buffer, size, 1); + __scsi_buffer_io(dev, lba, buffer, size, 1); } \ No newline at end of file diff --git a/lunaix-os/hal/pci.c b/lunaix-os/hal/pci.c index 4bf4c56..b03c128 100644 --- a/lunaix-os/hal/pci.c +++ b/lunaix-os/hal/pci.c @@ -187,7 +187,7 @@ pci_setup_msi(struct pci_device* device, int vector) pci_reg_t reg1 = pci_read_cspace(device->cspace_base, device->msi_loc); // manipulate the MSI_CTRL to allow device using MSI to request service. - reg1 = ((((reg1 >> 16) & ~0x70) | 0x1) << 16) | (reg1 & 0xffff); + reg1 = (reg1 & 0xff8fffff) | 0x10000; pci_write_cspace(device->cspace_base, device->msi_loc, reg1); } diff --git a/lunaix-os/includes/hal/ahci/hba.h b/lunaix-os/includes/hal/ahci/hba.h index e7f8d01..91ecfed 100644 --- a/lunaix-os/includes/hal/ahci/hba.h +++ b/lunaix-os/includes/hal/ahci/hba.h @@ -32,6 +32,7 @@ #define HBA_PxCMD_ST (1) #define HBA_PxINTR_DMA (1 << 2) #define HBA_PxINTR_D2HR (1) +#define HBA_PxINTR_DPE (1 << 5) #define HBA_PxTFD_ERR (1) #define HBA_PxTFD_BSY (1 << 7) #define HBA_PxTFD_DRQ (1 << 3) @@ -104,15 +105,16 @@ struct hba_device uint32_t alignment_offset; uint32_t block_per_sec; uint32_t capabilities; + struct hba_port* port; struct { - int (*identify)(struct hba_port* port); - int (*read_buffer)(struct hba_port* port, + int (*identify)(struct hba_device* dev); + int (*read_buffer)(struct hba_device* dev, uint64_t lba, void* buffer, uint32_t size); - int (*write_buffer)(struct hba_port* port, + int (*write_buffer)(struct hba_device* dev, uint64_t lba, void* buffer, uint32_t size); diff --git a/lunaix-os/includes/lunaix/mm/valloc.h b/lunaix-os/includes/lunaix/mm/valloc.h index d5d0c75..9be750c 100644 --- a/lunaix-os/includes/lunaix/mm/valloc.h +++ b/lunaix-os/includes/lunaix/mm/valloc.h @@ -5,7 +5,10 @@ void* valloc(unsigned int size); void* -vcalloc(unsigned int size); +vzalloc(unsigned int size); + +void* +vcalloc(unsigned int size, unsigned int count); void vfree(void* ptr); @@ -14,7 +17,7 @@ void* valloc_dma(unsigned int size); void* -vcalloc_dma(unsigned int size); +vzalloc_dma(unsigned int size); void vfree_dma(void* ptr); diff --git a/lunaix-os/kernel/mm/cake.c b/lunaix-os/kernel/mm/cake.c index 7e3142b..f6e4340 100644 --- a/lunaix-os/kernel/mm/cake.c +++ b/lunaix-os/kernel/mm/cake.c @@ -120,7 +120,6 @@ cake_grab(struct cake_pile* pile) pos = list_entry(pile->free.next, typeof(*pos), cakes); } -found: piece_index_t found_index = pos->next_free; pos->next_free = pos->free_list[found_index]; pos->used_pieces++; @@ -140,7 +139,7 @@ found: int cake_release(struct cake_pile* pile, void* area) { - piece_index_t area_index; + piece_index_t piece_index; struct cake_s *pos, *n; struct llist_header* hdrs[2] = { &pile->full, &pile->partial }; @@ -150,9 +149,9 @@ cake_release(struct cake_pile* pile, void* area) if (pos->first_piece > area) { continue; } - area_index = + piece_index = (uintptr_t)(area - pos->first_piece) / pile->piece_size; - if (area_index < pile->pieces_per_cake) { + if (piece_index < pile->pieces_per_cake) { goto found; } } @@ -161,8 +160,8 @@ cake_release(struct cake_pile* pile, void* area) return 0; found: - pos->free_list[area_index] = pos->next_free; - pos->next_free = area_index; + pos->free_list[piece_index] = pos->next_free; + pos->next_free = piece_index; pos->used_pieces--; pile->alloced_pieces--; diff --git a/lunaix-os/kernel/mm/valloc.c b/lunaix-os/kernel/mm/valloc.c index 631f39e..02384ef 100644 --- a/lunaix-os/kernel/mm/valloc.c +++ b/lunaix-os/kernel/mm/valloc.c @@ -67,13 +67,26 @@ valloc(unsigned int size) } void* -vcalloc(unsigned int size) +vzalloc(unsigned int size) { void* ptr = __valloc(size, &piles); memset(ptr, 0, size); return ptr; } +void* +vcalloc(unsigned int size, unsigned int count) +{ + unsigned int alloc_size; + if (__builtin_umul_overflow(size, count, &alloc_size)) { + return 0; + } + + void* ptr = __valloc(alloc_size, &piles); + memset(ptr, 0, alloc_size); + return ptr; +} + void vfree(void* ptr) { @@ -87,7 +100,7 @@ valloc_dma(unsigned int size) } void* -vcalloc_dma(unsigned int size) +vzalloc_dma(unsigned int size) { void* ptr = __valloc(size, &piles_dma); memset(ptr, 0, size); diff --git a/lunaix-os/kernel/mm/vmap.c b/lunaix-os/kernel/mm/vmap.c index 5df524d..deb18d3 100644 --- a/lunaix-os/kernel/mm/vmap.c +++ b/lunaix-os/kernel/mm/vmap.c @@ -27,7 +27,7 @@ vmm_vmap(uintptr_t paddr, size_t size, pt_attr attr) } else { x86_page_table* ptd = (x86_page_table*)(L2_VADDR(l1inx)); size_t i = L2_INDEX(current_addr); - for (; i < 1024 && examed_size < size; i++) { + for (; i < PG_MAX_ENTRIES && examed_size < size; i++) { if (!ptd->entry[i]) { examed_size += PG_SIZE; } else if (examed_size) { diff --git a/lunaix-os/kernel/proc0.c b/lunaix-os/kernel/proc0.c index 759db2c..36d8727 100644 --- a/lunaix-os/kernel/proc0.c +++ b/lunaix-os/kernel/proc0.c @@ -119,7 +119,7 @@ void __test_disk_io() { struct hba_port* port = ahci_get_port(0); - char* buffer = vcalloc_dma(port->device->block_size); + char* buffer = vzalloc_dma(port->device->block_size); strcpy(buffer, test_sequence); kprintf("WRITE: %s\n", buffer); int result;