Author SHA1 Message Date
sanyakapoor27 77281bb836 build: fix alloc (#6) (#11)
Does not fixes `hwa` example in production

Reviewed-on: Rust/Core#11
Co-authored-by: Sanya Kapoor <sanyakapoor2705@gmail.com>
Co-committed-by: Sanya Kapoor <sanyakapoor2705@gmail.com>
2026-03-20 10:45:37 +00:00
Sweetbread 5ca63e7afb fix: Add strlen func 2026-03-14 22:29:39 +03:00
Sweetbread 3e8244f686 config: update things and add shell.nix 2026-03-14 22:29:24 +03:00
12 changed files with 62 additions and 162 deletions

No files matched your search

+1
View File
@@ -1,5 +1,6 @@
[unstable]
build-std = ["core", "compiler_builtins", "alloc"]
json-target-spec = true
[build]
target = "i686-kolibri.json"
-3
View File
@@ -21,6 +21,3 @@ path = "examples/dark.rs"
[profile.release]
opt-level = "z"
lto = "thin"
[[example]]
name = "fs_demo"
-12
View File
@@ -1,12 +0,0 @@
#![no_std]
#![no_main]
use kos::fs::fs_read;
#[no_mangle]
pub extern "C" fn _start() -> ! {
let path = c"/rd/1/readme.txt";
let mut buf = [0u8; 512];
let (_status, _bytes) = fs_read(path, &mut buf);
loop {}
}
+2 -3
View File
@@ -5,7 +5,6 @@
"dynamic-linking": false,
"executables": true,
"has-rpath": true,
"is-builtin": false,
"linker-flavor": "ld.lld",
"linker": "rust-lld",
"llvm-target": "i686-unknown-none-code32",
@@ -14,9 +13,9 @@
"relocation-model": "static",
"position-independent-executables": false,
"relro-level": "off",
"target-c-int-width": "32",
"target-c-int-width": 32,
"target-endian": "little",
"target-pointer-width": "32",
"target-pointer-width": 32,
"vendor": "unknown",
"disable-redzone": true,
"panic-strategy": "abort",
+4 -1
View File
@@ -1 +1,4 @@
nightly
[toolchain]
channel = "nightly"
components = ["rust-src", "llvm-tools-preview"]
profile = "default"
+14
View File
@@ -0,0 +1,14 @@
{ pkgs ? import <nixpkgs> {} }:
pkgs.mkShell {
packages = with pkgs; [
rustup
cargo-make
cargo-binutils
fasm
];
shellHook = ''
export PATH="$HOME/.cargo/bin:$PATH"
'';
}
+31 -22
View File
@@ -36,6 +36,7 @@ pub fn init() {
unsafe {
sys::init_heap();
MAIN_SECTOR = sys::alloc(PAGE_SIZE) as usize;
core::ptr::write_bytes(MAIN_SECTOR as *mut u8, 0, PAGE_SIZE); // make sector table start clean
}
}
}
@@ -47,14 +48,14 @@ pub fn malloc(size: usize) -> *mut u8 {
if (addr as usize) != 0 {
let sec = addr;
let mut hdr = *(addr as *const SectorHeader);
let hdr = &mut *(addr as *mut SectorHeader);
let sec_start_blocks = (sec as usize) + size_of::<SectorHeader>();
if hdr.size_left >= size {
let mut j = sec_start_blocks;
let mut first_found_block_addr = 0;
while j <= sec_start_blocks + hdr.size {
let mut block = *(j as *const BlockHeader);
let block = &mut *(j as *mut BlockHeader);
match block.sign {
Sign::Active => {
// If block is occupated - pass
@@ -63,7 +64,7 @@ pub fn malloc(size: usize) -> *mut u8 {
}
Sign::Free => {
if first_found_block_addr != 0 {
if first_found_block_addr == 0 {
first_found_block_addr = j;
}
@@ -73,26 +74,32 @@ pub fn malloc(size: usize) -> *mut u8 {
j += (size_of::<BlockHeader>()) + block.size;
} else if size - sum_size < size_of::<BlockHeader>() {
// Create 2 blocks
let mut main_block =
*(first_found_block_addr as *const BlockHeader);
let main_block =
&mut *(first_found_block_addr as *mut BlockHeader);
main_block.sign = Sign::Active;
main_block.size = size;
let mut secondary_block = *(first_found_block_addr
as *const BlockHeader)
.add(size_of::<BlockHeader>() + size);
secondary_block.sign = Sign::Free;
secondary_block.size =
let secondary_block = (first_found_block_addr
+ size_of::<BlockHeader>()
+ size)
as *mut BlockHeader;
(*secondary_block).sign = Sign::Free;
(*secondary_block).size =
sum_size - size - size_of::<BlockHeader>();
hdr.size_left -= size + size_of::<BlockHeader>();
return (first_found_block_addr as *mut u8)
.add(size_of::<BlockHeader>());
} else {
// Create 1 block
let mut main_block =
*(first_found_block_addr as *const BlockHeader);
let main_block =
&mut *(first_found_block_addr as *mut BlockHeader);
main_block.sign = Sign::Active;
main_block.size = sum_size - size_of::<BlockHeader>();
hdr.size_left -= main_block.size + size_of::<BlockHeader>();
return (first_found_block_addr as *mut u8)
.add(size_of::<BlockHeader>());
}
@@ -117,9 +124,11 @@ pub fn malloc(size: usize) -> *mut u8 {
}
}
} else {
let sec_size = size + PAGE_SIZE - size % PAGE_SIZE;
// round requested size up to whole pages
let sec_size = (size + PAGE_SIZE - 1) & !(PAGE_SIZE - 1);
let new_sec = sys::alloc(sec_size);
let sec_hdr = new_sec as *mut SectorHeader;
*((MAIN_SECTOR + i * 4) as *mut u32) = new_sec as u32; // remember this sector in the table
*sec_hdr = SectorHeader {
size: sec_size,
size_left: sec_size - size_of::<SectorHeader>(),
@@ -138,26 +147,26 @@ pub fn malloc(size: usize) -> *mut u8 {
fn free(block: *const u8) {
unsafe {
let mut block_hdr = *(block.sub(size_of::<BlockHeader>()) as *mut BlockHeader);
let block_hdr = &mut *(block.sub(size_of::<BlockHeader>()) as *mut BlockHeader);
for i in 0..PAGE_SIZE / 4 {
let addr = *((MAIN_SECTOR + i * 4) as *const u32) as *const u8;
let mut hdr = *(addr as *const SectorHeader);
if addr.is_null() {
continue;
}
let hdr = &mut *(addr as *mut SectorHeader);
if addr < block && (block as usize) < (addr as usize) + hdr.size {
hdr.size_left += block_hdr.size;
hdr.size_left += block_hdr.size + size_of::<BlockHeader>();
if hdr.size_left == hdr.size - size_of::<SectorHeader>() {
free(addr)
sys::free(addr); // whole sector is free, hand it back to OS
*((MAIN_SECTOR + i * 4) as *mut u32) = 0; // and drop from the table
} else {
block_hdr.sign = Sign::Free;
}
break;
}
}
if !sys::free(block) {
panic!("Free failed");
}
}
}
@@ -181,4 +190,4 @@ unsafe impl alloc::alloc::GlobalAlloc for GlobalAlloc {
}
#[global_allocator]
static ALLOC: GlobalAlloc = GlobalAlloc;
static ALLOC: GlobalAlloc = GlobalAlloc;
+1 -83
View File
@@ -6,23 +6,10 @@ pub struct Console {
con_init: extern "stdcall" fn(u32, u32, u32, u32, *const u8),
con_write_string: extern "stdcall" fn(*const u8, u32),
con_exit: extern "stdcall" fn(bool),
con_set_title: extern "stdcall" fn(*const u8),
con_get_flags: extern "stdcall" fn() -> u32,
con_set_flags: extern "stdcall" fn(u32) -> u32,
con_get_font_height: extern "stdcall" fn() -> u32,
con_get_cursor_height: extern "stdcall" fn() -> u32,
con_set_cursor_height: extern "stdcall" fn(u32) -> u32,
con_getch: extern "stdcall" fn() -> u32,
con_getch2: extern "stdcall" fn() -> u16,
con_kbhit: extern "stdcall" fn() -> u32,
con_gets: extern "stdcall" fn(*mut u8, u32) -> *mut u8,
con_cls: extern "stdcall" fn(),
con_get_cursor_pos: extern "stdcall" fn(*mut u32, *mut u32),
con_set_cursor_pos: extern "stdcall" fn(u32, u32),
}
impl Console {
pub fn import(path: Option<&CStr>) -> Result<Self, &'static str> {
pub fn import(path: Option<&CStr>) -> Result<Self, &str> {
let lib = DLL::load_dll(path.unwrap_or(c"/sys/lib/console.obj"));
match lib {
Err(e) => return Err(e),
@@ -31,19 +18,6 @@ impl Console {
con_init: transmute(dll.get_func(c"con_init").ok().unwrap()),
con_write_string: transmute(dll.get_func(c"con_write_string").ok().unwrap()),
con_exit: transmute(dll.get_func(c"con_exit").ok().unwrap()),
con_set_title: transmute(dll.get_func(c"con_set_title").ok().unwrap()),
con_get_flags: transmute(dll.get_func(c"con_get_flags").ok().unwrap()),
con_set_flags: transmute(dll.get_func(c"con_set_flags").ok().unwrap()),
con_get_font_height: transmute(dll.get_func(c"con_get_font_height").ok().unwrap()),
con_get_cursor_height: transmute(dll.get_func(c"con_get_cursor_height").ok().unwrap()),
con_set_cursor_height: transmute(dll.get_func(c"con_set_cursor_height").ok().unwrap()),
con_getch: transmute(dll.get_func(c"con_getch").ok().unwrap()),
con_getch2: transmute(dll.get_func(c"con_getch2").ok().unwrap()),
con_kbhit: transmute(dll.get_func(c"con_kbhit").ok().unwrap()),
con_gets: transmute(dll.get_func(c"con_gets").ok().unwrap()),
con_cls: transmute(dll.get_func(c"con_cls").ok().unwrap()),
con_get_cursor_pos: transmute(dll.get_func(c"con_get_cursor_pos").ok().unwrap()),
con_set_cursor_pos: transmute(dll.get_func(c"con_set_cursor_pos").ok().unwrap()),
})
},
}
@@ -60,60 +34,4 @@ impl Console {
pub fn exit(self, close_window: bool) {
(self.con_exit)(close_window);
}
pub fn set_title(&self, title: &CStr) {
(self.con_set_title)(title.as_ptr() as *const u8);
}
pub fn get_flags(&self) -> u32 {
(self.con_get_flags)()
}
pub fn set_flags(&self, flags: u32) -> u32 {
(self.con_set_flags)(flags)
}
pub fn get_font_height(&self) -> u32 {
(self.con_get_font_height)()
}
pub fn get_cursor_height(&self) -> u32 {
(self.con_get_cursor_height)()
}
pub fn set_cursor_height(&self, height: u32) -> u32 {
(self.con_set_cursor_height)(height)
}
pub fn getch(&self) -> u32 {
(self.con_getch)()
}
pub fn getch2(&self) -> u16 {
(self.con_getch2)()
}
pub fn kbhit(&self) -> u32 {
(self.con_kbhit)()
}
pub fn gets<'a>(&self, buf: &'a mut [u8]) -> Option<&'a [u8]> {
let ptr = (self.con_gets)(buf.as_mut_ptr(), buf.len() as u32);
if ptr.is_null() { None } else { Some(buf) }
}
pub fn cls(&self) {
(self.con_cls)();
}
pub fn get_cursor_pos(&self) -> (u32, u32) {
let mut x = 0u32;
let mut y = 0u32;
(self.con_get_cursor_pos)(&mut x, &mut y);
(x, y)
}
pub fn set_cursor_pos(&self, x: u32, y: u32) {
(self.con_set_cursor_pos)(x, y);
}
}
-14
View File
@@ -1,14 +0,0 @@
use core::ffi::CStr;
use crate::sys::{syscall_fs, FileSystemRequest};
pub fn fs_read(path: &CStr, buf: &mut [u8]) -> (u32, u32) {
let req = FileSystemRequest {
operation: 0,
offset_low: 0,
offset_high: 0,
byte_count: buf.len() as u32,
buffer: buf.as_mut_ptr() as u32,
path: path.as_ptr() as u32,
};
unsafe { syscall_fs(&req) }
}
-1
View File
@@ -6,7 +6,6 @@ mod nanolibc;
pub mod allocation;
pub mod dll;
pub mod sys;
pub mod fs;
pub use modules::*;
+9
View File
@@ -31,3 +31,12 @@ pub unsafe extern "C" fn memcmp(s1: *mut u8, s2: *const u8, n: usize) -> i32 {
0
}
#[no_mangle]
pub unsafe extern "C" fn strlen(str: *mut u8) -> usize {
let mut len = 0usize;
loop {
if *str.add(len) == 0 { return len; }
len += 1;
}
}
-23
View File
@@ -68,26 +68,3 @@ extern "C" {
#[link_name = "_load_dll"]
pub fn load_dll(name: *const u8) -> *const u32;
}
#[repr(C)]
pub struct FileSystemRequest {
pub operation: u32,
pub offset_low: u32,
pub offset_high: u32,
pub byte_count: u32,
pub buffer: u32,
pub path: u32,
}
pub unsafe fn syscall_fs(req: *const FileSystemRequest) -> (u32, u32) {
let eax_result: u32;
let ebx_result: u32;
core::arch::asm!(
"int 0x40",
inout("eax") 70u32 => eax_result,
in("ebx") req,
out("ebx") ebx_result,
options(nostack)
);
(eax_result, ebx_result)
}