NOVA
Stripped down NOVA kernel for the OSY course
cpu.h
1 /*
2  * Central Processing Unit (CPU)
3  *
4  * Copyright (C) 2009-2011 Udo Steinberg <udo@hypervisor.org>
5  * Economic rights: Technische Universitaet Dresden (Germany)
6  *
7  * This file is part of the NOVA microhypervisor.
8  *
9  * NOVA is free software: you can redistribute it and/or modify it
10  * under the terms of the GNU General Public License version 2 as
11  * published by the Free Software Foundation.
12  *
13  * NOVA is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License version 2 for more details.
17  */
18 
19 #pragma once
20 
21 #include "compiler.h"
22 #include "types.h"
23 
24 class Cpu
25 {
26  public:
27  enum
28  {
29  EXC_NM = 7, /* No Math Coprocessor */
30  EXC_TS = 10, /* Invalid TSS */
31  EXC_GP = 13, /* General Protection */
32  EXC_PF = 14, /* Page Fault */
33  EXC_AC = 17 /* Alignment Check */
34  };
35 
36  ALWAYS_INLINE
37  static inline mword cr3()
38  {
39  mword cr3;
40  asm volatile ("mov %%cr3, %0" : "=r"(cr3));
41  return cr3;
42  }
43 
44  ALWAYS_INLINE
45  static inline void flush ()
46  {
47  mword cr3;
48  asm volatile ("mov %%cr3, %0; mov %0, %%cr3" : "=&r" (cr3));
49  }
50 
51  ALWAYS_INLINE
52  static inline void flush (mword addr)
53  {
54  asm volatile ("invlpg %0" : : "m" (*reinterpret_cast<mword *>(addr)));
55  }
56 
57  ALWAYS_INLINE
58  static inline void preempt_disable()
59  {
60  asm volatile ("cli" : : : "memory");
61  }
62 
63  ALWAYS_INLINE
64  static inline void preempt_enable()
65  {
66  asm volatile ("sti" : : : "memory");
67  }
68 
69  ALWAYS_INLINE NORETURN
70  static inline void shutdown()
71  {
72  for (;;)
73  asm volatile ("cli; hlt");
74  }
75 
76  ALWAYS_INLINE
77  static inline void cpuid (unsigned leaf, uint32 &eax, uint32 &ebx, uint32 &ecx, uint32 &edx)
78  {
79  asm volatile ("cpuid" : "=a" (eax), "=b" (ebx), "=c" (ecx), "=d" (edx) : "a" (leaf));
80  }
81 
82  ALWAYS_INLINE
83  static inline void cpuid (unsigned leaf, unsigned subleaf, uint32 &eax, uint32 &ebx, uint32 &ecx, uint32 &edx)
84  {
85  asm volatile ("cpuid" : "=a" (eax), "=b" (ebx), "=c" (ecx), "=d" (edx) : "a" (leaf), "c" (subleaf));
86  }
87 };
Definition: cpu.h:25