]> rtime.felk.cvut.cz Git - l4.git/blob - kernel/fiasco/src/test/wrappers/fake_kmem.cpp
Inital import
[l4.git] / kernel / fiasco / src / test / wrappers / fake_kmem.cpp
1 INTERFACE:
2
3 #include <cstddef>              // size_t
4 #include <flux/x86/types.h>     // vm_offset_t & friends
5 #include <flux/x86/multiboot.h> // multiboot_info
6 #include <flux/x86/paging.h>    // pd_entry_t
7
8 #include "kip.h"
9
10 //#include "config_gdt.h"
11
12 /* our own implementation of C++ memory management: disallow dynamic
13    allocation (except where class-specific new/delete functions exist) */
14
15 void *operator new(size_t);
16 void operator delete(void *block);
17
18 // more specialized memory allocation/deallocation functions follow
19 // below in the "kmem" namespace
20
21 // kernel.ld definitions
22
23 extern "C" {
24   extern char _start, _edata, _end;
25   // extern char _physmem_1;
26   // extern char _tcbs_1;
27   // extern char _iobitmap_1;
28   extern char _unused1_1, _unused2_1, _unused3_1, _unused4_io_1;
29   extern char _ipc_window0_1, _ipc_window1_1;
30 }
31
32 #if 0
33 struct x86_gate;
34 struct x86_tss;
35 struct x86_desc;
36 #endif
37
38 // the kernel memory is a singleton object.  we access it through a
39 // static class interface.  (we would have preferred to access it like
40 // a class, but our method saves the overhead of always passing the
41 // "this" pointer on the (very small) kernel stack).
42
43 // this object is the system's page-level allocator
44 class kmem
45 {
46 public:
47   // constants from the above kernel.ld declarations
48 //   static const vm_offset_t mem_phys = reinterpret_cast<vm_offset_t>(&_physmem_1);
49   static const vm_offset_t mem_phys = 0;
50 //   static const vm_offset_t mem_tcbs = reinterpret_cast<vm_offset_t>(&_tcbs_1);
51   static const vm_offset_t mem_user_max = 0xc0000000;
52
53   static vm_offset_t io_bitmap;
54
55   static const vm_offset_t ipc_window0 
56     = reinterpret_cast<vm_offset_t>(&_ipc_window0_1);
57   static const vm_offset_t ipc_window1 
58     = reinterpret_cast<vm_offset_t>(&_ipc_window1_1);
59
60 //   enum { gdt_tss = GDT_TSS, 
61 //     gdt_code_kernel = GDT_CODE_KERNEL, gdt_data_kernel = GDT_DATA_KERNEL,
62 //     gdt_code_user = GDT_CODE_USER, gdt_data_user = GDT_DATA_USER, 
63 //     gdt_max = GDT_MAX };
64
65 // protected:
66 //   static pd_entry_t *kdir;   // kernel page directory
67 //   static pd_entry_t cpu_global;
68
69 private:
70 //   friend class kdb;
71 //   friend class jdb;
72 //   friend class profile;
73
74   kmem();                       // default constructors are undefined
75   kmem(const kmem&);
76
77 //   static vm_offset_t mem_max, _himem;
78
79   static const pd_entry_t flag_global = 0x200;  // l4-specific pg dir entry flag
80
81 //   static x86_gate *kidt;
82 //   static x86_tss *tss;
83 //   static x86_desc *gdt;
84
85   static Kip *kinfo;
86
87 //   static multiboot_info kmbi;
88 //   static char kcmdline[256];
89 };
90
91 IMPLEMENTATION:
92
93 #include <cassert>
94 #include <cstdio>
95 #include <cstring>
96 #include <cstdlib>
97
98 // #include <flux/x86/tss.h>
99 // #include <flux/x86/seg.h>
100 // #include <flux/x86/proc_reg.h>
101 // #include <flux/x86/gate_init.h>
102 // #include <flux/x86/base_vm.h>
103
104
105 #include "globals.h"
106 // #include "entry.h"
107 // #include "irq_init.h"
108 // #include "config.h"
109
110 #include "unix_aligned_alloc.h"
111
112 //
113 // class kmem
114 //
115
116 // static class variables
117
118 // vm_offset_t kmem::mem_max, kmem::_himem;
119 // pd_entry_t *kmem::kdir;
120 // pd_entry_t  kmem::cpu_global;
121
122 static Kip the_kinfo =
123   {
124     /* magic: */ L4_KERNEL_INFO_MAGIC,
125     /* version: */ 0x01004444,
126     0, {0,},
127     0, 0, 0, 0,
128     0, 0, {0,},
129     0, 0, {0,},
130     0, 0, {0,},
131     0, 0, 0, 0, 
132 // main_memory.high: mem_max,
133     /* main_memory: */ { 0, 0x4000000},
134 // reserved0.low = trunc_page(virt_to_phys(&__crt_dummy__)),
135 // reserved0.high = round_page(virt_to_phys(&_end)),
136     /* reserved0: */ { 0, 0},
137     /* reserved1: */ { 0, 0},
138     /* semi_reserved: */ { 1024 * 640, 1024 * 1024},
139 // Skipped rest -- rely on 0-initialization.
140     /* clock: 0 */
141   };
142
143 Kip *kmem::kinfo = &the_kinfo;
144
145 vm_offset_t kmem::io_bitmap 
146   = reinterpret_cast<vm_offset_t>
147       (unix_aligned_alloc(2 * PAGE_SIZE + 4,  2* PAGE_SIZE));
148
149 // multiboot_info kmem::kmbi;
150 // char kmem::kcmdline[256];
151
152 // x86_gate *kmem::kidt;
153 // x86_tss *kmem::tss;
154 // x86_desc *kmem::gdt;
155
156 // 
157 // kmem virtual-space related functions (similar to space_t)
158 // 
159 PUBLIC static
160 vm_offset_t 
161 kmem::virt_to_phys(const void *addr)
162 {
163   return reinterpret_cast<vm_offset_t>(addr);
164 }
165
166 PUBLIC static
167 inline void *kmem::phys_to_virt(vm_offset_t addr) // physical to kernel-virtual
168 {
169   return reinterpret_cast<void *>(addr);
170 }
171
172 // initialize a new task's page directory with the current master copy
173 // of kernel page tables
174 PUBLIC static
175 void kmem::dir_init(pd_entry_t *d)
176 {
177   // Do nothing.
178 }
179
180 // 
181 // TLB flushing
182 // 
183 PUBLIC static
184 inline void kmem::tlb_flush(vm_offset_t addr) // flush tlb at virtual addr
185 {
186   // Do nothing
187 }
188
189 PUBLIC static
190 inline void kmem::tlb_flush()
191 {
192   // Do nothing
193 }
194
195 //
196 // ACCESSORS
197 //
198 PUBLIC static
199 inline vm_offset_t 
200 kmem::ipc_window(unsigned win)
201 {
202   if (win == 0)
203     return ipc_window0;
204
205   return ipc_window1;
206 }
207
208 PUBLIC static
209 inline Kip *kmem::info() // returns the kernel info page
210 {
211   return kinfo;
212 }
213
214 PUBLIC static
215 const pd_entry_t *kmem::dir() // returns the kernel's page directory
216 {
217   assert (! "Cannot request kernel's page directory.");
218   return 0;
219   //  return kdir;
220 }
221
222 PUBLIC static
223 pd_entry_t kmem::pde_global() // returns mask for global pg dir entries
224 {
225   // return cpu_global;
226   return flag_global 
227     | ((cpu.feature_flags & CPUF_PAGE_GLOBAL_EXT)
228        ? INTEL_PDE_GLOBAL 
229        : 0);
230 }
231
232 // PUBLIC static
233 // inline const multiboot_info *kmem::mbi()
234 // {
235 //   return &kmbi;
236 // }
237
238 // PUBLIC static
239 // inline const char *kmem::cmdline()
240 // {
241 //   return kcmdline;
242 // }
243
244 // PUBLIC static
245 // inline x86_gate *kmem::idt()
246 // {
247 //   return kidt;
248 // }
249
250 // PUBLIC static
251 // inline vm_offset_t kmem::himem()
252 // {
253 //   return _himem;
254 // }
255
256 // // Get a pointer to the CPUs kernel stack pointer
257 // PUBLIC static
258 // inline NEEDS [<flux/x86/tss.h>]
259 // vm_offset_t *kmem::kernel_esp()
260 // {
261 //   return reinterpret_cast<vm_offset_t*>(& tss->esp0);
262 // }