1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272

#![no_std]
#![cfg_attr(test, no_main)]
#![allow(dead_code)]
#![allow(named_asm_labels)]
#![allow(unused_imports)]
#![allow(unused_variables)]
#![feature(abi_x86_interrupt)]
#![feature(alloc_error_handler)]
#![feature(asm_sym)]
#![feature(const_mut_refs)]
#![feature(core_intrinsics)]
#![feature(custom_test_frameworks)]
#![feature(naked_functions)]
#![feature(slice_range)]
#![feature(type_ascription)]
#![test_runner(crate::testexec)]
#![reexport_test_harness_main = "testmain"]


extern crate alloc;
extern crate core;

use core::{ops::Deref, panic::PanicInfo};

pub mod libcore;
pub mod clock;
pub mod cmos;
pub mod ctypes;
pub mod font;
pub mod init;
pub mod intr;
pub mod macros;
pub mod mem;
pub mod noblkio;
pub mod pic;
pub mod rgx;
pub mod ser;
pub mod time;
pub mod vol;

// This is set to be 2MB.
pub const KSIZE: usize = 2 << 20;

#[alloc_error_handler]
fn alloc_error_handler(layout: alloc::alloc::Layout) -> !
{
	panic!("[ERR] ALLOC ERR: {:?}", layout)
}

pub fn init()
{
	libcore::sys::gdt::init();
	intr::idtinit();
	unsafe
	{
		intr::PICS.lock().init()
	};
	x86_64::instructions::interrupts::enable();
}

pub fn hltloop() -> !
{
	loop
	{
		x86_64::instructions::hlt();
	}
}

// This implementation is used for the FAT filesystem.
pub trait BlockDevice
{
	type Error;
	fn read(&self, buf: &mut [u8], address: usize, numblk: usize) -> Result<(), Self::Error>;
	fn write(&self, buf: &[u8], address: usize, numblk: usize) -> Result<(), Self::Error>;
}

pub trait CanTest
{
	fn exec(&self) -> ();
}

impl<T> CanTest for T
where
	T: Fn(),
{
	fn exec(&self)
	{
		serprint!("{}...\t", core::any::type_name::<T>());
		self();
		serprintln!("[SUCCESS]");
	}
}

pub fn testexec(tests: &[&dyn CanTest])
{
	serprintln!("RUNNING {} TESTS:", tests.len());
	for test in tests
	{
		test.exec();
	}
	exitqemu(QEMUExitCode::Success);
}

pub fn test_panic_handler(info: &PanicInfo) -> !
{
	serprintln!("[FAILURE]\n");
	serprintln!("[ERR]: {}\n", info);
	exitqemu(QEMUExitCode::Failure);
	hltloop();
}

// This section provides the kernel wth the StableDeref trait.

pub unsafe trait StableDeref: Deref {}
pub unsafe trait CloneStableDeref: StableDeref + Clone {}

use alloc::borrow::Cow;
use alloc::boxed::Box;
use alloc::rc::Rc;
use alloc::string::String;
use alloc::sync::Arc;
use alloc::vec::Vec;

use core::cell::{Ref, RefMut};

unsafe impl<T: ?Sized> StableDeref for Box<T> {}
unsafe impl<T> StableDeref for Vec<T> {}
unsafe impl StableDeref for String {}
unsafe impl<'a> StableDeref for Cow<'a, str> {}
unsafe impl<'a, T: Clone> StableDeref for Cow<'a, [T]> {}
unsafe impl<T: ?Sized> StableDeref for Rc<T> {}
unsafe impl<T: ?Sized> CloneStableDeref for Rc<T> {}
unsafe impl<T: ?Sized> StableDeref for Arc<T> {}
unsafe impl<T: ?Sized> CloneStableDeref for Arc<T> {}
unsafe impl<'a, T: ?Sized> StableDeref for Ref<'a, T> {}
unsafe impl<'a, T: ?Sized> StableDeref for RefMut<'a, T> {}
unsafe impl<'a, T: ?Sized> StableDeref for &'a T {}
unsafe impl<'a, T: ?Sized> CloneStableDeref for &'a T {}
unsafe impl<'a, T: ?Sized> StableDeref for &'a mut T {}

// This section of lib.rs provides the kernel with the AsSlice and AsMutSlice
// traits of the standard library, minus the standard library.

pub trait AsSlice
{
	type Elem;
	fn as_slice(&self) -> &[Self::Elem];
}

pub trait AsMutSlice: AsSlice
{
	fn asmutslice(&mut self) -> &mut [Self::Elem];
}

impl<'a, S> AsSlice for &'a S
where
	S: ?Sized + AsSlice,
{
	type Elem = S::Elem;
	fn as_slice(&self) -> &[S::Elem]
	{
		(**self).as_slice()
	}
}

impl<'a, S> AsSlice for &'a mut S
where
	S: ?Sized + AsSlice,
{
	type Elem = S::Elem;
	fn as_slice(&self) -> &[S::Elem]
	{
		(**self).as_slice()
	}
}

impl<'a, S> AsMutSlice for &'a mut S
where
	S: ?Sized + AsMutSlice,
{
	fn asmutslice(&mut self) -> &mut [S::Elem]
	{
		(**self).asmutslice()
	}
}

impl<T> AsSlice for [T]
{
	type Elem = T;
	fn as_slice(&self) -> &[T]
	{
		self
	}
}

impl<T> AsMutSlice for [T]
{
	fn asmutslice(&mut self) -> &mut [T]
	{
		self
	}
}

impl<T, const N: usize> AsSlice for [T; N]
{
	type Elem = T;
	fn as_slice(&self) -> &[T]
	{
		self
	}
}

impl<T, const N: usize> AsMutSlice for [T; N]
{
	fn asmutslice(&mut self) -> &mut [T]
	{
		self
	}
}


// TESTING


#[cfg(test)]
use bootloader::{BootInfo, entry_point};

#[cfg(test)]
//entry_point!(test_kernmain);

#[cfg(test)]
fn test_kernmain(_bootinfo: &'static BootInfo) -> !
{
	init();
	testmain();
	hltloop();
}

#[cfg(test)]
#[no_mangle]
pub extern "C" fn _start() -> !
{
	init();
	testmain();
	hltloop();
}

#[cfg(test)]
#[panic_handler]
fn panic(info: &PanicInfo) -> !
{
	test_panic_handler(info)
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u32)]
pub enum QEMUExitCode
{
	Success = 0x10,
	Failure = 0x11,
}

pub fn exitqemu(exitcode: QEMUExitCode)
{
	use x86_64::instructions::port::Port;
	unsafe
	{
		let mut port = Port::new(0xf4);
		port.write(exitcode as u32);
	}
}