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
// Copyright (c) 2020 Alex Chi
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT

#![allow(non_camel_case_types)]

use crate::{println, panic, info};
use crate::process::{my_cpu, my_proc, Process, ProcessState};
use crate::spinlock::MutexGuard;

/// Kernel context
#[repr(C)]
pub struct Context {
    // ra + sp + s0 ~ s11
    pub regs: [usize; 14]
}

impl Context {
    pub const fn zero() -> Self {
        Self {
            regs: [0; 14]
        }
    }
}

pub enum ContextRegisters {
    ra = 0,
    sp = 1
}

extern "C" {
    /// __swtch in `swtch.S`
    fn __swtch(current: &mut Context, to: &Context);
}

/// switch context
pub fn swtch(current: &mut Context, next: Context) {
    unsafe { __swtch(current, &next); }
}

/// switch to scheduler context
pub fn sched() {
    let c = my_cpu();
    let p = my_proc();

    if p.state == ProcessState::RUNNING {
        panic!("process running");
    }

    if crate::arch::intr_get() {
        panic!("yield interruptable");
    }

    let ctx = core::mem::replace(&mut c.scheduler_context, Context::zero());

    let intena = c.intr_lock.is_enabled_before;

    swtch(&mut p.context, ctx);
    c.intr_lock.is_enabled_before = intena;
}

/// give up CPU to scheduler
pub fn yield_cpu() {
    let p = my_proc();
    p.state = ProcessState::RUNNABLE;
    sched();
}