Skip to content

Commit

Permalink
kernel: Move to linux ABI from mlibc ABI
Browse files Browse the repository at this point in the history
  • Loading branch information
mintsuki committed Dec 22, 2023
1 parent 6056f2e commit d6ca1e9
Show file tree
Hide file tree
Showing 19 changed files with 365 additions and 379 deletions.
34 changes: 24 additions & 10 deletions kernel/linker.ld
Original file line number Diff line number Diff line change
Expand Up @@ -4,52 +4,66 @@
* Copyright (C) 2021-2022 The Vinix authors.
*/

/* Tell the linker that we want an x86_64 ELF64 output file */
OUTPUT_FORMAT(elf64-x86-64)
OUTPUT_ARCH(i386:x86-64)

/* Tell the linker that we want the symbol main__kmain to be our entry point */
/* We want the symbol main__kmain to be our entry point */
ENTRY(main__kmain)

/* Define the program headers we want so the bootloader gives us the right */
/* MMU permissions */
PHDRS
{
null PT_NULL FLAGS(0) ;
text PT_LOAD FLAGS((1 << 0) | (1 << 2)) ;
rodata PT_LOAD FLAGS(1 << 2) ;
data PT_LOAD FLAGS((1 << 1) | (1 << 2)) ;
}

SECTIONS
{
/* We wanna be placed in the higher half, 2MiB above 0 in physical memory. */
/* We wanna be placed in the topmost 2GiB of the address space, for optimisations */
/* and because that is what the Limine spec mandates. */
/* Any address in this region will do, but often 0xffffffff80000000 is chosen as */
/* that is the beginning of the region. */
. = 0xffffffff80000000;

.text : {
*(.text .text.*)
} : text
} :text

/* Move to the next memory page for .rodata */
. += CONSTANT(MAXPAGESIZE);

/* Then let's place all the other traditional executable sections afterwards. */
.rodata : {
*(.rodata .rodata.*)
} : rodata
} :rodata

.symbol_table : {
*(.symbol_table)
} : rodata
} :rodata

/* Move to the next memory page for .data */
. += CONSTANT(MAXPAGESIZE);

.data : {
*(.data .data.*)
} : data
} :data

/* NOTE: .bss needs to be the last thing mapped to :data, otherwise lots of */
/* unnecessary zeros will be written to the binary. */
/* If you need, for example, .init_array and .fini_array, those should be placed */
/* above this. */
.bss : {
*(COMMON)
*(.bss .bss.*)
} : data
*(COMMON)
} :data

KERNEL_END_SYMBOL = .;

/* Discard .note.* and .eh_frame since they may cause issues on some hosts. */
/DISCARD/ : {
*(.eh_frame)
*(.note .note.*)
}
}
14 changes: 7 additions & 7 deletions kernel/modules/block/partition/partition.v
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,9 @@ fn (mut this Partition) mmap(page u64, flags int) voidptr {
}

pub fn scan_partitions(mut parent_device resource.Resource, prefix string) int {
lba_buffer := memory.malloc(parent_device.stat.blksize)
lba_buffer := memory.malloc(u64(parent_device.stat.blksize))

parent_device.read(0, lba_buffer, parent_device.stat.blksize, parent_device.stat.blksize) or {
parent_device.read(0, lba_buffer, u64(parent_device.stat.blksize), u64(parent_device.stat.blksize)) or {
print('block: unable to read from device\n')
return -1
}
Expand All @@ -122,7 +122,7 @@ pub fn scan_partitions(mut parent_device resource.Resource, prefix string) int {
if gpt_hdr.identifier == partition.gpt_signature {
entry_list_lba := gpt_hdr.partition_array_lba
entry_cnt := gpt_hdr.partition_entry_cnt
entry_list_size := lib.align_up(sizeof(GPTPartitionEntry) * entry_cnt, parent_device.stat.blksize)
entry_list_size := lib.align_up(sizeof(GPTPartitionEntry) * entry_cnt, u64(parent_device.stat.blksize))

if gpt_hdr.partition_entry_size != sizeof(GPTPartitionEntry) {
print('gpt: fatal parsing error\n')
Expand All @@ -131,7 +131,7 @@ pub fn scan_partitions(mut parent_device resource.Resource, prefix string) int {

partition_entry_buffer := memory.malloc(entry_list_size)

parent_device.read(0, partition_entry_buffer, entry_list_lba * parent_device.stat.blksize,
parent_device.read(0, partition_entry_buffer, u64(entry_list_lba * parent_device.stat.blksize),
entry_list_size) or {
print('block: unable to read from device\n')
return -1
Expand All @@ -148,7 +148,7 @@ pub fn scan_partitions(mut parent_device resource.Resource, prefix string) int {
}

mut partition := &Partition{
device_offset: partition_entry.starting_lba * parent_device.stat.blksize
device_offset: u64(partition_entry.starting_lba * parent_device.stat.blksize)
sector_cnt: partition_entry.last_lba - partition_entry.starting_lba
parent_device: unsafe { parent_device }
}
Expand All @@ -167,7 +167,7 @@ pub fn scan_partitions(mut parent_device resource.Resource, prefix string) int {
return 0
}

parent_device.read(0, lba_buffer, 0, parent_device.stat.blksize) or {
parent_device.read(0, lba_buffer, 0, u64(parent_device.stat.blksize)) or {
print('block: unable to read from device\n')
return -1
}
Expand All @@ -186,7 +186,7 @@ pub fn scan_partitions(mut parent_device resource.Resource, prefix string) int {
partition_entry := unsafe { &MBRPartition(&partitions[i]) }

mut partition := &Partition{
device_offset: partition_entry.starting_lba * parent_device.stat.blksize
device_offset: u64(partition_entry.starting_lba * parent_device.stat.blksize)
sector_cnt: partition_entry.sector_cnt
parent_device: unsafe { parent_device }
}
Expand Down
16 changes: 8 additions & 8 deletions kernel/modules/dev/ahci/ahci.v
Original file line number Diff line number Diff line change
Expand Up @@ -195,17 +195,17 @@ fn (mut dev AHCIDevice) read(handle voidptr, buffer voidptr, loc u64, count u64)
start_blk := loc / dev.stat.blksize
page_cnt := count / dev.stat.blksize

aligned_buffer := voidptr(u64(memory.pmm_alloc(page_cnt)) + higher_half)
aligned_buffer := voidptr(u64(memory.pmm_alloc(u64(page_cnt))) + higher_half)

if dev.rw_lba(aligned_buffer, start_blk, page_cnt, false) == -1 {
if dev.rw_lba(aligned_buffer, u64(start_blk), u64(page_cnt), false) == -1 {
errno.set(errno.eio)
memory.pmm_free(aligned_buffer, page_cnt)
memory.pmm_free(aligned_buffer, u64(page_cnt))
return none
}

unsafe { C.memcpy(buffer, aligned_buffer, count) }

memory.pmm_free(voidptr(u64(aligned_buffer) - higher_half), page_cnt)
memory.pmm_free(voidptr(u64(aligned_buffer) - higher_half), u64(page_cnt))

return i64(count)
}
Expand All @@ -219,16 +219,16 @@ fn (mut dev AHCIDevice) write(handle voidptr, buffer voidptr, loc u64, count u64
start_blk := loc / dev.stat.blksize
page_cnt := count / dev.stat.blksize

aligned_buffer := voidptr(u64(memory.pmm_alloc(page_cnt)) + higher_half)
aligned_buffer := voidptr(u64(memory.pmm_alloc(u64(page_cnt))) + higher_half)
unsafe { C.memcpy(aligned_buffer, buffer, count) }

if dev.rw_lba(aligned_buffer, start_blk, page_cnt, true) == -1 {
if dev.rw_lba(aligned_buffer, u64(start_blk), u64(page_cnt), true) == -1 {
errno.set(errno.eio)
memory.pmm_free(aligned_buffer, page_cnt)
memory.pmm_free(aligned_buffer, u64(page_cnt))
return none
}

memory.pmm_free(voidptr(u64(aligned_buffer) - higher_half), page_cnt)
memory.pmm_free(voidptr(u64(aligned_buffer) - higher_half), u64(page_cnt))

return i64(count)
}
Expand Down
16 changes: 8 additions & 8 deletions kernel/modules/dev/nvme/nvme.v
Original file line number Diff line number Diff line change
Expand Up @@ -356,17 +356,17 @@ fn (mut dev NVMENamespace) read(handle voidptr, buffer voidptr, loc u64, count u
start_blk := loc / dev.stat.blksize
page_cnt := count / dev.stat.blksize

aligned_buffer := voidptr(u64(memory.pmm_alloc(page_cnt)) + higher_half)
aligned_buffer := voidptr(u64(memory.pmm_alloc(u64(page_cnt))) + higher_half)

if dev.rw_lba(aligned_buffer, start_blk, page_cnt, false) == -1 {
if dev.rw_lba(aligned_buffer, u64(start_blk), u64(page_cnt), false) == -1 {
errno.set(errno.eio)
memory.pmm_free(aligned_buffer, page_cnt)
memory.pmm_free(aligned_buffer, u64(page_cnt))
return none
}

unsafe { C.memcpy(buffer, aligned_buffer, count) }

memory.pmm_free(voidptr(u64(aligned_buffer) - higher_half), page_cnt)
memory.pmm_free(voidptr(u64(aligned_buffer) - higher_half), u64(page_cnt))

return i64(count)
}
Expand All @@ -380,16 +380,16 @@ fn (mut dev NVMENamespace) write(handle voidptr, buffer voidptr, loc u64, count
start_blk := loc / dev.stat.blksize
page_cnt := count / dev.stat.blksize

aligned_buffer := voidptr(u64(memory.pmm_alloc(page_cnt)) + higher_half)
aligned_buffer := voidptr(u64(memory.pmm_alloc(u64(page_cnt))) + higher_half)
unsafe { C.memcpy(aligned_buffer, buffer, count) }

if dev.rw_lba(aligned_buffer, start_blk, page_cnt, true) == -1 {
if dev.rw_lba(aligned_buffer, u64(start_blk), u64(page_cnt), true) == -1 {
errno.set(errno.eio)
memory.pmm_free(aligned_buffer, page_cnt)
memory.pmm_free(aligned_buffer, u64(page_cnt))
return none
}

memory.pmm_free(voidptr(u64(aligned_buffer) - higher_half), page_cnt)
memory.pmm_free(voidptr(u64(aligned_buffer) - higher_half), u64(page_cnt))

return i64(count)
}
Expand Down
Loading

0 comments on commit d6ca1e9

Please sign in to comment.