Skip to content

Commit 1353c37

Browse files
committed
feat: implement thread (wip)
1 parent c9acaa3 commit 1353c37

File tree

7 files changed

+77
-33
lines changed

7 files changed

+77
-33
lines changed

src/arch/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ macro_rules! with_arch {
2929
($arch: ident) => {
3030
mod $arch;
3131
pub use self::$arch::Initializer;
32+
pub use self::$arch::Thread;
3233
};
3334
}
3435

src/arch/x86_64/interrupt.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ mod handler;
66
mod pic;
77
mod pit;
88
mod table;
9+
pub use handler::InterruptFrame;
910
use table::InterruptDescriptorTable;
1011

1112
// 0x00007E00 - 0x0007FFFF is free region.

src/arch/x86_64/interrupt/handler.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ bitfield! {
1515
#[repr(C)]
1616
pub struct InterruptFrame {
1717
ip: usize,
18-
cs: usize,
18+
pub cs: usize,
1919
flags: usize,
2020
sp: usize,
21-
ss: usize,
21+
pub ss: usize,
2222
}
2323

2424
impl fmt::Debug for InterruptFrame {
@@ -27,15 +27,15 @@ impl fmt::Debug for InterruptFrame {
2727
}
2828
}
2929

30-
pub type Handler = extern "x86-interrupt" fn(&InterruptFrame);
31-
pub type HandlerWithErrorCode = extern "x86-interrupt" fn(&InterruptFrame, ErrorCode);
30+
pub type Handler = extern "x86-interrupt" fn(&mut InterruptFrame);
31+
pub type HandlerWithErrorCode = extern "x86-interrupt" fn(&mut InterruptFrame, ErrorCode);
3232

33-
pub extern "x86-interrupt" fn default_handler(interrupt_frame: &InterruptFrame) {
33+
pub extern "x86-interrupt" fn default_handler(interrupt_frame: &mut InterruptFrame) {
3434
println!("Got interrupt");
3535
println!(" {:?}", interrupt_frame);
3636
}
3737

38-
pub extern "x86-interrupt" fn default_handler_with_error_code(interrupt_frame: &InterruptFrame, error: ErrorCode) {
38+
pub extern "x86-interrupt" fn default_handler_with_error_code(interrupt_frame: &mut InterruptFrame, error: ErrorCode) {
3939
println!("Got interrupt");
4040
println!(" {:?}", interrupt_frame);
4141
println!(" {:x}", error.0);

src/arch/x86_64/interrupt/pic.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use super::super::thread;
12
use super::handler::InterruptFrame;
23
use super::table::InterruptDescriptorTable;
34
use lazy_static::lazy_static;
@@ -71,9 +72,10 @@ pub unsafe fn enable_timer_interrupt() {
7172
MASTER_DATA_PORT.lock().write(0b1111_1110);
7273
}
7374

74-
pub extern "x86-interrupt" fn timer_handler(_: &InterruptFrame) {
75+
pub extern "x86-interrupt" fn timer_handler(frame: &mut InterruptFrame) {
7576
// FIXME: use listener pattern.
76-
crate::process::switch_process();
77+
thread::switch_context(frame);
78+
7779
send_end_of_interrupt(IRQ_NUMBER_TIMER);
7880
}
7981

src/arch/x86_64/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ use lazy_static::lazy_static;
88

99
mod interrupt;
1010
mod tss;
11+
mod thread;
12+
13+
pub use thread::Thread;
1114

1215
const VRAM_ADDR: PhysicalAddress = 0xB8000;
1316

src/arch/x86_64/thread.rs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
use super::interrupt::InterruptFrame;
2+
3+
pub struct Thread {}
4+
5+
impl Thread {
6+
pub fn new() -> Thread {
7+
Thread {}
8+
}
9+
}
10+
11+
pub fn switch_context(frame: &mut InterruptFrame) {
12+
crate::process::select_threads(|| {
13+
frame.cs = 8 * 3 + 3;
14+
frame.ss = 8 * 4 + 3;
15+
unsafe {
16+
asm!("mov ds, $0
17+
mov es, $0
18+
mov fs, $0
19+
mov gs, $0
20+
"
21+
:
22+
: "r"(frame.ss as u16)
23+
: "ax"
24+
: "intel", "volatile"
25+
);
26+
}
27+
28+
// let mut current_ip = 0;
29+
// let mut current_sp = 0;
30+
// let mut next_ip = 0;
31+
// let mut next_sp = 0;
32+
// unsafe {
33+
// asm!("
34+
// pushf
35+
//
36+
// mov [$0], $2
37+
// mov [$1], rsp
38+
// "
39+
// : "=r"(&mut current_ip) "=r"(&mut current_sp)
40+
// : "r"(landing_point as usize)
41+
// : "memory"
42+
// : "intel", "volatile");
43+
// }
44+
//
45+
// println!("{}", current_ip);
46+
// println!("{}", current_sp);
47+
})
48+
}
49+
50+
// #[no_mangle]
51+
// pub extern "C" fn landing_point() {
52+
// unsafe { asm!("popf") }
53+
// }

src/process.rs

Lines changed: 9 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::arch::Thread;
12
use crate::memory::{ActivePageTable, FrameAllocator, InActivePageTable};
23
use core::mem;
34
use core::ptr;
@@ -6,13 +7,15 @@ use spin::Mutex;
67

78
struct Process {
89
page_table: InActivePageTable,
10+
thread: Thread,
911
}
1012

1113
impl Process {
1214
fn new(allocator: &mut FrameAllocator) -> Process {
1315
// FIXME: remove 1st argument (ActivePageTable).
1416
Process {
1517
page_table: InActivePageTable::new(unsafe { &mut ActivePageTable::new() }, allocator).unwrap(),
18+
thread: Thread::new(),
1619
}
1720
}
1821
}
@@ -40,29 +43,10 @@ lazy_static! {
4043
static ref PROCESS_MANAGER: Mutex<ProcessManager> = Mutex::new(ProcessManager::new());
4144
}
4245

43-
pub fn switch_process() {
44-
// let mut current_ip = 0;
45-
// let mut current_sp = 0;
46-
// let mut next_ip = 0;
47-
// let mut next_sp = 0;
48-
// unsafe {
49-
// asm!("
50-
// pushf
51-
//
52-
// mov [$0], $2
53-
// mov [$1], rsp
54-
// "
55-
// : "=r"(&mut current_ip) "=r"(&mut current_sp)
56-
// : "r"(landing_point as usize)
57-
// : "memory"
58-
// : "intel", "volatile");
59-
// }
60-
//
61-
// println!("{}", current_ip);
62-
// println!("{}", current_sp);
46+
pub fn select_threads<F>(mut switch_context: F)
47+
where
48+
F: FnMut(),
49+
{
50+
// TODO: pass current process and next process.
51+
switch_context();
6352
}
64-
65-
// #[no_mangle]
66-
// pub extern "C" fn landing_point() {
67-
// unsafe { asm!("popf") }
68-
// }

0 commit comments

Comments
 (0)