aboutsummaryrefslogtreecommitdiff
path: root/src/system.rs
blob: 4e48303060ad40e0218d47d125fbaee170046d4c (plain)
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
use std;
use std::cell::RefCell;
use std::rc::Rc;

use rustv::isa;
use rustv::memory::SharedMemory;
use rustv::register_file::RegisterFile;
use rustv::syscall;
use rustv::trap::Trap;

use shareable_cache::ShareableCache;

pub struct SyscallHandler<'a> {
    memory: SharedMemory<'a>,
    caches: Vec<Rc<RefCell<ShareableCache<'a>>>>,
}

impl<'a> SyscallHandler<'a> {
    pub fn new(memory: SharedMemory<'a>,
               caches: Vec<Rc<RefCell<ShareableCache<'a>>>>)
               -> SyscallHandler<'a> {
        SyscallHandler {
            memory: memory,
            caches: caches,
        }
    }
}

impl<'a> SyscallHandler<'a> {
    fn prints(&mut self, core_id: usize,
             registers: &mut RegisterFile) -> Option<Trap> {
        let mut base = registers.read_word(isa::Register::X11);
        let mut string = vec![];

        loop {
            let c = self.memory.borrow_mut().read_byte(base);

            if let Ok(0x00) = c {
                break;
            }
            else if let Ok(c) = c {
                string.push(c);
            }

            base += 1;
        }

        let result = std::str::from_utf8(&string);
        if let Ok(string) = result {
            println!("{}", string);
        }
        else {
            println!("Error printing string: not valid UTF-8: {:?}", result);
        }
        None
    }

    fn printi(&mut self, core_id: usize,
              registers: &mut RegisterFile) -> Option<Trap> {
        println!("{}", registers.read_word(isa::Register::X11));

        None
    }

    fn enable_secondary(&mut self, core_id: usize,
                        registers: &mut RegisterFile) -> Option<Trap> {
        debug!("[syscall] [memory] Secondary cache enabled for core {}",
               core_id);
        self.caches[core_id].borrow_mut().enable_secondary();
        None
    }

    fn disable_secondary(&mut self, core_id: usize,
                         registers: &mut RegisterFile) -> Option<Trap> {
        debug!("[syscall] [memory] Secondary cache disabled for core {}",
               core_id);
        self.caches[core_id].borrow_mut().disable_secondary();
        None
    }
}

impl<'a> syscall::SyscallHandler for SyscallHandler<'a> {
    // TODO: is trap really appropriate here?
    fn syscall(&mut self, core_id: usize,
               registers: &mut RegisterFile) -> Option<Trap> {
        let syscall_number = registers.read_word(isa::Register::X10);

        match syscall_number {
            22 => self.prints(core_id, registers),
            23 => self.printi(core_id, registers),
            24 => self.enable_secondary(core_id, registers),
            25 => self.disable_secondary(core_id, registers),

            _ => {
                // TODO: some sort of error reporting
                None
            }
        }
    }
}