-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscheduler.rs
More file actions
682 lines (605 loc) · 21.4 KB
/
Copy pathscheduler.rs
File metadata and controls
682 lines (605 loc) · 21.4 KB
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
//! # Scheduler
//!
//! A preemptive, multitasking process scheduler.
use {
crate::{
KERNEL_STACK, KERNEL_STACK_SIZE, gdt,
loader::{LoadedObject, global_loader},
memory::{AddressSpace, KernelMapping, kernel_address_space},
},
alloc::{
collections::{btree_map::BTreeMap, vec_deque::VecDeque},
string::String,
sync::Arc,
},
core::{
arch::asm,
fmt,
sync::atomic::{AtomicU64, Ordering},
},
log::{debug, info},
memory_types::{Address, AddressDomain, PAGE_SIZE, PageRange, PageTableFlags},
process::{AccessPolicy, Priority, ShellInput, ShellOutput, ShellQueue},
spin_mutex::Mutex,
x86_64::{
instructions::interrupts::without_interrupts,
registers::{
control::{Cr0, Cr0Flags, Cr4, Cr4Flags},
rflags::RFlags,
},
structures::idt::InterruptStackFrameValue,
},
};
const IDLE_PROCESS_ID: u64 = 0;
pub const DEFAULT_KERNEL_STACK_SIZE: usize = PAGE_SIZE * 8;
pub const DEFAULT_USER_STACK_SIZE: usize = PAGE_SIZE * 16;
static PROCESS_ID: AtomicU64 = AtomicU64::new(IDLE_PROCESS_ID + 1);
pub fn init() {
unsafe {
let cr0 = Cr0::read();
if cr0.contains(Cr0Flags::EMULATE_COPROCESSOR) {
Cr0::write(cr0 & !Cr0Flags::EMULATE_COPROCESSOR);
info!("Cleared CR0.EM");
}
let cr4 = Cr4::read();
if !cr4.contains(Cr4Flags::OSFXSR | Cr4Flags::OSXMMEXCPT_ENABLE) {
Cr4::write(cr4);
info!("Set CR4.OSFXSR and CR4.OSXMMEXCPT");
}
const MSCSR_OFFSET: usize = 0x18;
asm!("fninit", options(nostack, preserves_flags));
fxsave(&mut INIT_FXSAVE_AREA);
INIT_FXSAVE_AREA.bytes[MSCSR_OFFSET] = 0x80;
INIT_FXSAVE_AREA.bytes[MSCSR_OFFSET + 1] = 0x1F;
fxrstor(&INIT_FXSAVE_AREA);
}
}
/// Start preemptive multitasking.
pub fn run() -> ! {
SCHEDULER.lock().init();
schedule()
}
/// Define an interrupt handler that makes use of the [`ExecutionContext`] at
/// the time of interruption.
#[macro_export]
macro_rules! define_interrupt_handler_with_context {
(| $name:ident | $body:block) => {
#[unsafe(naked)]
pub extern "x86-interrupt" fn $name(
frame: ::x86_64::structures::idt::InterruptStackFrame,
) {
use $crate::scheduler::{schedule, with_scheduler, ExecutionContext};
::core::arch::naked_asm!(
// Assemble an `ExecutionContext` into `rdi` (first function argument).
"push rax",
"push rbx",
"push rcx",
"push rdx",
"push rbp",
"push rdi",
"push rsi",
"push r8",
"push r9",
"push r10",
"push r11",
"push r12",
"push r13",
"push r14",
"push r15",
"mov rdi, rsp",
// Now that we've properly assembled the context, we can jump to `__handler`.
"call {}",
sym __handler,
);
extern "C" fn __handler(context: ExecutionContext) -> ! {
// Interrupts should not be enabled at this point, but it can't hurt to make sure.
assert!(!::x86_64::instructions::interrupts::are_enabled());
// Make sure the current context is the one that was executing before this
// interrupt started.
with_scheduler(|scheduler| scheduler.set_current_context(context));
// Run whatever the `body` needs to run.
$body
// Schedule the next process to run.
schedule()
}
}
};
}
/// The global [`Scheduler`] instance.
static SCHEDULER: Mutex<Scheduler> = Mutex::new(Scheduler::new());
/// The kernel's process scheduler.
pub struct Scheduler {
current: Option<Process>,
queue: BTreeMap<Priority, VecDeque<Process>>,
blocked: VecDeque<Process>,
shell_queue: Option<(Arc<Mutex<KernelMapping>>, usize)>,
}
impl Scheduler {
const fn new() -> Self {
Self {
current: None,
queue: BTreeMap::new(),
blocked: VecDeque::new(),
shell_queue: None,
}
}
fn init(&mut self) {
fn __idle_loop() -> ! {
loop {
x86_64::instructions::hlt();
}
}
self.add_to_queue(Process {
id: IDLE_PROCESS_ID,
name: "idle".into(),
address_space: AddressSpace::new("idle", None),
access_policy: AccessPolicy::All,
priority: Priority::Idle,
context: Some(ExecutionContext {
registers: CpuRegisters::EMPTY,
frame: InterruptStackFrameValue::new(
x86_64::VirtAddr::from_ptr(__idle_loop as *const fn() -> !),
gdt::selectors().kernel_code,
RFlags::INTERRUPT_FLAG,
x86_64::VirtAddr::from_ptr(unsafe {
KERNEL_STACK.as_ptr().add(KERNEL_STACK_SIZE).sub(8)
}),
gdt::selectors().kernel_code,
),
}),
heap_mapping: None,
allow_io: true,
fxsave_area: unsafe { INIT_FXSAVE_AREA },
});
let shell_input_section = global_loader()
.get_section("shell::QUEUE")
.expect("shell::QUEUE should be loaded at this point")
.upgrade()
.expect("shell::QUEUE dropped before scheduler could access it");
self.shell_queue = Some((
shell_input_section.mapping.clone(),
shell_input_section.mapping_offset,
));
}
/// Get the currently running [`Process`].
pub fn current_process(&self) -> Option<&Process> {
self.current.as_ref()
}
/// Get a mutable reference the currently running [`Process`].
pub fn current_process_mut(&mut self) -> Option<&mut Process> {
self.current.as_mut()
}
fn add_to_queue(&mut self, process: Process) {
self.queue
.entry(process.priority)
.or_default()
.push_back(process);
}
fn schedule_next(&mut self) -> ExecutionContext {
// Handle the next queued message from the shell.
kernel_address_space().enter();
if let Some(output) = self
.shell_queue
.as_mut()
.and_then(|(mapping, offset)| unsafe {
mapping
.try_lock()
.unwrap()
.as_mut::<ShellQueue>(*offset)
.output
.lock()
.pop()
})
{
match output {
ShellOutput::ExitProcess { code } => {
let process = self
.blocked
.pop_front()
.expect("shell output should correspond to a blocked process");
log::trace!("Exiting `{}` with code {code}...", process.name);
}
ShellOutput::StartProcess { name } => {
// TODO: Disallow I/O access by default.
self.run_user_process(name.as_str(), None, true, AccessPolicy::Normal);
}
ShellOutput::AllowModuleAccess { addr, process_id } => {
let process = self
.blocked
.pop_front()
.expect("shell output should correspond to a blocked process");
assert_eq!(process_id, process.id);
if let Some(section) = global_loader()
.get_section_for_addr(Address::new(addr))
.and_then(|weak| weak.upgrade())
{
let mapping = section.mapping.lock();
if let Err(error) =
mapping.map_into(&process.address_space, mapping.pages, mapping.flags)
{
log::error!(
"Failed to map `{}` into `{}` at {addr:x}: {error}",
section.name,
process.address_space.name(),
);
} else {
self.queue
.entry(process.priority)
.or_default()
.push_back(process);
}
} else {
log::error!("Got invalid module access output for {process} at {addr:x}",);
}
}
}
}
// Make sure to (re-)enter the current address space after polling the shell above.
if let Some(process) = &self.current {
process.address_space.enter();
} else {
let process = self
.queue
.values_mut()
.find(|q| !q.is_empty())
.expect("should at least have an idle process available")
.pop_front()
.unwrap();
process.address_space.enter();
crate::gdt::set_user_io_allowed(process.allow_io);
unsafe {
fxrstor(process.fxsave_area.bytes.as_ptr() as _);
}
self.current = Some(process);
}
self.current
.as_mut()
.expect("current process should exist")
.context
.take()
.expect("current process should have a context")
}
/// Set the current process's [`ExecutionContext`] and save the current
/// [`FxSaveArea`].
pub fn set_current_context(&mut self, context: ExecutionContext) {
let process = self
.current
.as_mut()
.expect("a process should be running at this point");
let prev_context = process.context.replace(context);
assert!(prev_context.is_none());
unsafe {
fxsave(process.fxsave_area.bytes.as_mut_ptr() as _);
}
}
/// Preempt the currently running process, and place it at the end of the
/// run queue.
pub fn preempt_current(&mut self) {
let process = self
.current
.take()
.expect("current process should be available for preemption");
self.add_to_queue(process);
}
/// Block the currently running process, and force it to wait on a [`ShellOutput`]
/// for the given [`ShellInput`].
pub fn block_current(&mut self, request: ShellInput) {
let process = self
.current
.take()
.expect("current process should be available to be blocked");
// log::trace!("Blocking {process} with request {input:x?}...");
kernel_address_space().enter();
self.blocked.push_back(process);
self.shell_queue
.as_mut()
.map(|(mapping, offset)| unsafe {
mapping
.try_lock()
.unwrap()
.as_mut::<ShellQueue>(*offset)
.input
.lock()
.push(request);
})
.expect("shell should be ready");
}
/// Add a kernel process with the given parameters to the run queue.
///
/// ## Arguments
///
/// - `name`, the name of the process to be run.
/// - `entry_point`, a pointer to the entry point of the process. The function must be
/// diverging.
/// - `stack_size`, a size for the new process's stack. If `None` is provided, the
/// [`DEFAULT_KERNEL_STACK_SIZE`] will be used.
#[allow(unused)]
pub fn run_kernel_process(
&mut self,
name: impl Into<String>,
entry_point: *const fn() -> !,
stack_size: Option<usize>,
) {
let id = PROCESS_ID.fetch_add(1, Ordering::SeqCst);
let name = name.into();
// This is a kernel process running kernel code, so just inherit the kernel's
// address space.
let address_space = AddressSpace::new(format!("{name}.{id}"), Some(kernel_address_space()));
let stack_size = stack_size.unwrap_or(DEFAULT_KERNEL_STACK_SIZE);
let stack_top_addr = AddressDomain::UserCode.base_addr();
address_space.map_pages(
format!("kernel_stack.{id}"),
PageRange::from_end_size(stack_top_addr.page(), stack_size),
PageTableFlags::PRESENT | PageTableFlags::WRITABLE,
);
let context = ExecutionContext {
registers: CpuRegisters::EMPTY,
frame: InterruptStackFrameValue::new(
x86_64::VirtAddr::from_ptr(entry_point),
gdt::selectors().kernel_code,
RFlags::INTERRUPT_FLAG,
x86_64::VirtAddr::new(stack_top_addr.to_raw() as u64 - 8),
gdt::selectors().kernel_data,
),
};
let process = Process {
id,
name,
access_policy: AccessPolicy::All,
priority: Priority::Normal,
address_space,
context: Some(context),
heap_mapping: None,
allow_io: true,
fxsave_area: unsafe { INIT_FXSAVE_AREA },
};
info!("Running {process}");
self.add_to_queue(process);
}
/// Add a user process with the given parameters to the run queue.
///
/// ## Arguments
///
/// - `name`, the name of the process to be run.
/// - `stack_size`, a size for the new process's stack. If `None` is provided, the
/// [`DEFAULT_USER_STACK_SIZE`] will be used.
/// - `allow_io`, whether the new process will be allowed to perform I/O instructions.
pub fn run_user_process(
&mut self,
name: impl Into<String>,
stack_size: Option<usize>,
allow_io: bool,
access_policy: AccessPolicy,
) -> Arc<Mutex<LoadedObject>> {
let id = PROCESS_ID.fetch_add(1, Ordering::SeqCst);
let name = name.into();
let address_space = AddressSpace::new(format!("{name}.{id}"), None);
let user_code_addr = AddressDomain::UserCode.base_addr();
let object = global_loader()
.load_object(&name, &address_space, user_code_addr.page())
.unwrap();
let entry_point_section = global_loader()
.get_text_section(&name, "main")
.unwrap()
.upgrade()
.unwrap();
let entry_point = user_code_addr + entry_point_section.mapping_offset;
let stack_size = stack_size.unwrap_or(DEFAULT_USER_STACK_SIZE);
let stack_top_addr = AddressDomain::UserCode.base_addr();
assert!(stack_top_addr.to_raw() % 16 == 0);
address_space
.map_pages(
format!("user_stack.{id}"),
PageRange::from_end_size(stack_top_addr.page(), stack_size),
PageTableFlags::PRESENT
| PageTableFlags::WRITABLE
| PageTableFlags::USER_ACCESSIBLE
| PageTableFlags::NO_EXECUTE,
)
.unwrap();
let context = ExecutionContext {
registers: CpuRegisters::EMPTY,
frame: InterruptStackFrameValue::new(
x86_64::VirtAddr::new(entry_point.to_raw() as u64),
gdt::selectors().user_code,
RFlags::INTERRUPT_FLAG,
x86_64::VirtAddr::new(stack_top_addr.to_raw() as u64 - 8),
gdt::selectors().user_data,
),
};
let process = Process {
id,
name,
access_policy,
priority: Priority::Normal,
address_space,
context: Some(context),
heap_mapping: None,
allow_io,
fxsave_area: unsafe { INIT_FXSAVE_AREA },
};
info!("Running {process}");
self.add_to_queue(process);
object
}
}
/// Perform some operation on the [`Scheduler`] with interrupts disabled.
pub fn with_scheduler<F, R>(op: F) -> R
where
F: FnOnce(&mut Scheduler) -> R,
{
without_interrupts(|| {
let mut scheduler = SCHEDULER.lock();
op(&mut *scheduler)
})
}
/// Schedule the next process to run.
pub fn schedule() -> ! {
let next_context = with_scheduler(Scheduler::schedule_next);
unsafe {
asm!(
"mov rsp, {}",
"pop r15",
"pop r14",
"pop r13",
"pop r12",
"pop r11",
"pop r10",
"pop r9",
"pop r8",
"pop rsi",
"pop rdi",
"pop rbp",
"pop rdx",
"pop rcx",
"pop rbx",
"pop rax",
"iretq",
in(reg) &next_context,
options(noreturn),
)
}
}
pub const DEFER_INTERRUPT_NUMBER: u8 = 0x40; // TODO: Choose a less arbitrary number.
pub const EXIT_INTERRUPT_NUMBER: u8 = 0x41;
pub const TRANSLATE_ADDR_INTERRUPT_NUMBER: u8 = 0x42;
define_interrupt_handler_with_context!(|defer_interrupt_handler| {
with_scheduler(|scheduler| scheduler.preempt_current());
});
define_interrupt_handler_with_context!(|exit_interrupt_handler| {
// Exiting the current process is as simple as dropping it. The process
// will no longer exist within the run queue, and its allocated frames will
// be deallocated when the address space is dropped.
let Some(mut process) = with_scheduler(|scheduler| scheduler.current.take()) else {
unreachable!()
};
let context = process
.context
.take()
.expect("process should have a context on exit");
let exit_code = context.registers.rdi as i64;
kernel_address_space().enter();
info!("Exiting {process} with code: {}", exit_code);
debug!("CONTEXT: {context:x?}");
// Immediately after this block is finished, `Scheduler::schedule_next`
// is called to set the next execution context.
});
define_interrupt_handler_with_context!(|translate_addr_interrupt_handler| {
with_scheduler(|scheduler| {
let Some(current) = &mut scheduler.current else {
unreachable!();
};
let Some(context) = &mut current.context else {
unreachable!();
};
// TODO: Check permissions here.
let virt_addr = Address::new(context.registers.rdi as usize);
if let Some(phys_addr) = current.address_space.translate_address(virt_addr) {
// log::trace!(
// "Translating {virt_addr:x} >> {phys_addr:x} for `{}`",
// current.name,
// );
context.registers.rax = phys_addr.to_raw() as u64;
} else {
context.registers.rax = (-2_i64).cast_unsigned();
}
});
});
/// Exit the current process.
pub fn exit(code: i64) -> ! {
unsafe {
core::arch::asm!("int 0x41", in("rdi") code, options(nostack, nomem, noreturn));
}
}
/// The state of a running process.
#[derive(Debug)]
pub struct Process {
/// The ID of the process.
pub id: u64,
/// The name of the process. For user processes, this is the basename of the
/// process's object file (e.g. "example" for "/example.o").
pub name: String,
/// The [`AccessPolicy`] of the process.
pub access_policy: AccessPolicy,
/// The run [`Priority`] of the process.
priority: Priority,
/// The [`AddressSpace`] of the process. That is, everything the process can
/// "see" in virtual memory.
pub address_space: AddressSpace,
/// The [`ExecutionContext`] of the process. If this is `None`, the process
/// is currently running.
pub context: Option<ExecutionContext>,
/// The [`KernelMapping`] corresponding to the process's heap.
pub heap_mapping: Option<KernelMapping>,
/// Whether the process is allowed to perform I/O instructions.
allow_io: bool,
pub fxsave_area: FxSaveArea,
}
impl fmt::Display for Process {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_fmt(format_args!("Process #{} '{}'", self.id, self.name))
}
}
/// All information necessary for preempting/resuming a [`Process`].
#[derive(Debug)]
#[repr(C)]
pub struct ExecutionContext {
pub registers: CpuRegisters,
pub frame: InterruptStackFrameValue,
}
#[derive(Clone, Debug)]
#[repr(C)]
pub struct CpuRegisters {
pub r15: u64,
pub r14: u64,
pub r13: u64,
pub r12: u64,
pub r11: u64,
pub r10: u64,
pub r9: u64,
pub r8: u64,
pub rsi: u64,
pub rdi: u64,
pub rbp: u64,
pub rdx: u64,
pub rcx: u64,
pub rbx: u64,
pub rax: u64,
}
impl CpuRegisters {
const EMPTY: Self = Self {
r15: 0,
r14: 0,
r13: 0,
r12: 0,
r11: 0,
r10: 0,
r9: 0,
r8: 0,
rsi: 0,
rdi: 0,
rbp: 0,
rdx: 0,
rcx: 0,
rbx: 0,
rax: 0,
};
}
#[repr(C, align(64))]
#[derive(Clone, Copy, Debug)]
pub struct FxSaveArea {
bytes: [u8; 512],
}
static mut INIT_FXSAVE_AREA: FxSaveArea = FxSaveArea { bytes: [0; 512] };
#[inline]
unsafe fn fxsave(area: *mut FxSaveArea) {
unsafe {
asm!("fxsave64 [{0}]", in(reg) area, options(nostack, preserves_flags));
}
}
#[inline]
unsafe fn fxrstor(area: *const FxSaveArea) {
unsafe {
asm!("fxrstor64 [{0}]", in(reg) area, options(nostack, preserves_flags));
}
}