4 Commits
13 changed files with 122 additions and 61 deletions

No files matched your search

-1
View File
@@ -1,6 +1,5 @@
[unstable]
build-std = ["core", "compiler_builtins", "alloc"]
json-target-spec = true
[build]
target = "i686-kolibri.json"
+3
View File
@@ -21,3 +21,6 @@ path = "examples/dark.rs"
[profile.release]
opt-level = "z"
lto = "thin"
[[example]]
name = "fs_demo"
+10
View File
@@ -0,0 +1,10 @@
#![no_std]
#![no_main]
use kos::fs::read_file;
#[no_mangle]
pub fn kol_main() {
let mut buf = [0u8; 256];
let _ = read_file(c"/rd/1/readme.txt", &mut buf);
}
+3 -2
View File
@@ -5,6 +5,7 @@
"dynamic-linking": false,
"executables": true,
"has-rpath": true,
"is-builtin": false,
"linker-flavor": "ld.lld",
"linker": "rust-lld",
"llvm-target": "i686-unknown-none-code32",
@@ -13,9 +14,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",
+1 -4
View File
@@ -1,4 +1 @@
[toolchain]
channel = "nightly"
components = ["rust-src", "llvm-tools-preview"]
profile = "default"
nightly
-14
View File
@@ -1,14 +0,0 @@
{ pkgs ? import <nixpkgs> {} }:
pkgs.mkShell {
packages = with pkgs; [
rustup
cargo-make
cargo-binutils
fasm
];
shellHook = ''
export PATH="$HOME/.cargo/bin:$PATH"
'';
}
+22 -31
View File
@@ -36,7 +36,6 @@ 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
}
}
}
@@ -48,14 +47,14 @@ pub fn malloc(size: usize) -> *mut u8 {
if (addr as usize) != 0 {
let sec = addr;
let hdr = &mut *(addr as *mut SectorHeader);
let mut hdr = *(addr as *const 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 block = &mut *(j as *mut BlockHeader);
let mut block = *(j as *const BlockHeader);
match block.sign {
Sign::Active => {
// If block is occupated - pass
@@ -64,7 +63,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;
}
@@ -74,32 +73,26 @@ 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 main_block =
&mut *(first_found_block_addr as *mut BlockHeader);
let mut main_block =
*(first_found_block_addr as *const BlockHeader);
main_block.sign = Sign::Active;
main_block.size = size;
let secondary_block = (first_found_block_addr
+ size_of::<BlockHeader>()
+ size)
as *mut BlockHeader;
(*secondary_block).sign = Sign::Free;
(*secondary_block).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 =
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 main_block =
&mut *(first_found_block_addr as *mut BlockHeader);
let mut main_block =
*(first_found_block_addr as *const 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>());
}
@@ -124,11 +117,9 @@ pub fn malloc(size: usize) -> *mut u8 {
}
}
} else {
// round requested size up to whole pages
let sec_size = (size + PAGE_SIZE - 1) & !(PAGE_SIZE - 1);
let sec_size = size + PAGE_SIZE - size % PAGE_SIZE;
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>(),
@@ -147,26 +138,26 @@ pub fn malloc(size: usize) -> *mut u8 {
fn free(block: *const u8) {
unsafe {
let block_hdr = &mut *(block.sub(size_of::<BlockHeader>()) as *mut BlockHeader);
let mut block_hdr = *(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;
if addr.is_null() {
continue;
}
let hdr = &mut *(addr as *mut SectorHeader);
let mut hdr = *(addr as *const SectorHeader);
if addr < block && (block as usize) < (addr as usize) + hdr.size {
hdr.size_left += block_hdr.size + size_of::<BlockHeader>();
hdr.size_left += block_hdr.size;
if hdr.size_left == hdr.size - size_of::<SectorHeader>() {
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
free(addr)
} else {
block_hdr.sign = Sign::Free;
}
break;
}
}
if !sys::free(block) {
panic!("Free failed");
}
}
}
@@ -190,4 +181,4 @@ unsafe impl alloc::alloc::GlobalAlloc for GlobalAlloc {
}
#[global_allocator]
static ALLOC: GlobalAlloc = GlobalAlloc;
static ALLOC: GlobalAlloc = GlobalAlloc;
+58
View File
@@ -0,0 +1,58 @@
use core::ffi::CStr;
use crate::sys::{fs_execute, FsBlock};
fn execute(op: u32, offset: u32, offset_hi: u32, count: u32, buf: *mut u8, path: &CStr) -> Result<(), u32> {
let info = FsBlock {
operation: op,
offset,
offset_hi,
byte_count: count,
buffer: buf,
_pad: 0,
path: path.as_ptr() as *const u8,
};
match unsafe { fs_execute(&info) } {
0 => Ok(()),
e => Err(e),
}
}
pub fn read_file(path: &CStr, buf: &mut [u8]) -> Result<(), u32> {
execute(0, 0, 0, buf.len() as u32, buf.as_mut_ptr(), path)
}
pub fn read_folder(path: &CStr, buf: &mut [u8]) -> Result<(), u32> {
execute(1, 0, 0, buf.len() as u32, buf.as_mut_ptr(), path)
}
pub fn create_file(path: &CStr, buf: &[u8]) -> Result<(), u32> {
execute(2, 0, 0, buf.len() as u32, buf.as_ptr() as *mut u8, path)
}
pub fn write_file(path: &CStr, buf: &[u8]) -> Result<(), u32> {
execute(3, 0, 0, buf.len() as u32, buf.as_ptr() as *mut u8, path)
}
pub fn set_size(path: &CStr, size: u64) -> Result<(), u32> {
execute(4, size as u32, (size >> 32) as u32, 0, core::ptr::null_mut(), path)
}
pub fn get_attributes(path: &CStr, buf: &mut [u8]) -> Result<(), u32> {
execute(5, 0, 0, buf.len() as u32, buf.as_mut_ptr(), path)
}
pub fn set_attributes(path: &CStr, buf: &[u8]) -> Result<(), u32> {
execute(6, 0, 0, buf.len() as u32, buf.as_ptr() as *mut u8, path)
}
pub fn run(path: &CStr) -> Result<(), u32> {
execute(7, 0, 0, 0, core::ptr::null_mut(), path)
}
pub fn delete(path: &CStr) -> Result<(), u32> {
execute(8, 0, 0, 0, core::ptr::null_mut(), path)
}
pub fn create_folder(path: &CStr) -> Result<(), u32> {
execute(9, 0, 0, 0, core::ptr::null_mut(), path)
}
+1
View File
@@ -20,3 +20,4 @@ SF_SYS_MISC = 68
SSF_MEM_ALLOC = 12
SSF_MEM_FREE = 13
SSF_LOAD_DLL = 19
SF_FILE = 70
+1
View File
@@ -6,6 +6,7 @@ mod nanolibc;
pub mod allocation;
pub mod dll;
pub mod sys;
pub mod fs;
pub use modules::*;
-9
View File
@@ -31,12 +31,3 @@ 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;
}
}
+15
View File
@@ -67,4 +67,19 @@ extern "C" {
// 68.19
#[link_name = "_load_dll"]
pub fn load_dll(name: *const u8) -> *const u32;
// 70
#[link_name = "_fs_execute"]
pub fn fs_execute(info: *const FsBlock) -> u32;
}
#[repr(C)]
pub(crate) struct FsBlock {
pub operation: u32,
pub offset: u32,
pub offset_hi: u32,
pub byte_count: u32,
pub buffer: *mut u8,
pub _pad: u8,
pub path: *const u8,
}
+8
View File
@@ -20,6 +20,7 @@ section '.text'
public _get_lang
public _load_dll
public _set_event_mask
public _fs_execute
_exit:
mov eax, SF_TERMINATE_PROCESS
@@ -143,3 +144,10 @@ _set_event_mask:
int 0x40
ret
_fs_execute:
push ebx
mov eax, SF_FILE
mov ebx, [esp + 4 * 2]
int 0x40
pop ebx
ret