]> git.buserror.net Git - polintos/scott/priv.git/blob - kernel/tests/aspace.cc
random kernel stuff
[polintos/scott/priv.git] / kernel / tests / aspace.cc
1 // tests/aspace.cc
2 //
3 // This software is copyright (c) 2006 Scott Wood <scott@buserror.net>.
4 // 
5 // This software is provided 'as-is', without any express or implied warranty.
6 // In no event will the authors or contributors be held liable for any damages
7 // arising from the use of this software.
8 // 
9 // Permission is hereby granted to everyone, free of charge, to use, copy,
10 // modify, prepare derivative works of, publish, distribute, perform,
11 // sublicense, and/or sell copies of the Software, provided that the above
12 // copyright notice and disclaimer of warranty be included in all copies or
13 // substantial portions of this software.
14
15 #include <kern/types.h>
16 #include <kern/libc.h>
17 #include <kern/time.h>
18 #include <kern/thread.h>
19 #include <kern/mem.h>
20 #include <kern/process.h>
21
22 using namespace Mem;
23 namespace Mem {
24         extern IMappable physmem;
25 }
26
27 void thread(void *arg)
28 {
29         // Yuck -- out param->return value promotion would turn it into
30         // AddrSpace aspace = AddrSpace::downcast(Mem::proc_addr_space_factory.create()),
31         // but that's still ugly.  Perhaps something like:
32         // AddrSpace aspace(Mem::proc_addr_space_factory).
33         
34         Object obj;
35         proc_addr_space_factory.create(&obj);
36         IAddrSpace aspace = IAddrSpace::downcast(obj);
37         
38         printf("aspace %p created\n", (void *)aspace);
39         
40         ProcAddrSpace *kaspace = static_cast<ProcAddrSpace *>(AddrSpace::classptr(aspace));
41         
42         printf("kaspace %p\n", kaspace);
43         
44         curthread->set_aspace(kaspace);
45         printf("Addr space set\n");
46
47         IAddrSpace stacked[2];
48         IMappable mappables[2];
49
50         for (int i = 0; i < 2; i++) {
51                 addr_space_factory.create(&obj);
52                 stacked[i] = IAddrSpace::downcast(obj);
53                 stacked[i].get_mappable(&mappables[i]);
54         }
55         
56         printf("stacked %p %p mappable %p %p\n", 
57                (void *)stacked[0], (void *)stacked[1],
58                (void *)mappables[0], (void *)mappables[1]);
59                 
60 //      char *buf = new char[100000];
61         char *buf = (char *)alloc_pages(25);
62         printf("buf %p\n", buf);
63         
64         char *abuf = (char *)((((ulong)buf) + 4095) & ~4095);
65         printf("abuf %p\n", abuf);
66         
67         memset(abuf, 'A', Arch::page_size);
68         memset(abuf + Arch::page_size, 'B', Arch::page_size);
69
70         Region region;
71         u64 vstart[6];
72         System::Mem::MapFlags mf;
73         mf.access_IDLNS_Read = 1;
74         mf.access_IDLNS_Write = 1;
75         mf.Fixed = 1;
76         
77         region.start = 0;
78         region.end = 0xffffffff;
79         vstart[4] = 0xdeadbeef00000000ULL;
80         vstart[5] = 0xdeadb12345678000ULL;
81         
82         stacked[0].map(Mem::physmem, region, &vstart[4], mf);
83         mf.CopyOnWrite = 1;
84         stacked[0].map(Mem::physmem, region, &vstart[5], mf);
85         mf.CopyOnWrite = 0;
86         printf("stacked[0] vstart %llx, %llx\n", vstart[4], vstart[5]);
87         
88         region.start = 0xdeadb00000000000ULL;
89         region.end   = 0xdeadbfffffffffffULL;
90         vstart[4]    = 0x1234500000000000ULL;
91         mf.Fixed = 0;
92 //      mf.access_IDLNS_Write = 0;
93         
94         stacked[1].map(mappables[0], region, &vstart[4], mf);
95
96         printf("stacked[1] vstart %llx\n", vstart[4]);
97         
98         region.start = vstart[4] + 0xeef00000000ULL + kvirt_to_phys(abuf);
99         region.end   = vstart[4] + 0xeef00000000ULL + kvirt_to_phys(abuf) + Arch::page_size - 1;
100         vstart[0] = System::Mem::AddrSpace_ns::unspecified_start;
101         vstart[1] = System::Mem::AddrSpace_ns::unspecified_start;
102         vstart[2] = System::Mem::AddrSpace_ns::unspecified_start;
103         mf.access_IDLNS_Write = 1;
104         
105         aspace.map(mappables[1], region, &vstart[0], mf);
106         
107         region.start += Arch::page_size;
108         region.end += Arch::page_size;
109         aspace.map(mappables[1], region, &vstart[1], mf);
110
111         region.start = vstart[4] + 0x12345678000ULL + kvirt_to_phys(abuf);
112         region.end   = vstart[4] + 0x12345678000ULL + kvirt_to_phys(abuf) + Arch::page_size - 1;
113
114         aspace.map(mappables[1], region, &vstart[2], mf);
115
116         mf.CopyOnWrite = 1;
117         aspace.map(mappables[1], region, &vstart[3], mf);
118
119         printf("vstart %llx %llx %llx %llx\n", vstart[0], vstart[1], vstart[2], vstart[3]);
120         char *vbuf[4];
121         vbuf[0] = (char *)vstart[0];
122         vbuf[1] = (char *)vstart[1];
123         vbuf[2] = (char *)vstart[2];
124         vbuf[3] = (char *)vstart[3];
125
126         printf("%c%c%c%c ", vbuf[0][0], vbuf[0][1], vbuf[0][2], vbuf[0][3]);
127         printf("%c%c%c%c ", vbuf[1][0], vbuf[1][1], vbuf[1][2], vbuf[1][3]);
128         printf("%c%c%c%c ", vbuf[2][0], vbuf[2][1], vbuf[2][2], vbuf[2][3]);
129         printf("%c%c%c%c\n", vbuf[3][0], vbuf[3][1], vbuf[3][2], vbuf[3][3]);
130         
131         vbuf[0][0] = 'a';
132         vbuf[1][1] = 'b';
133         vbuf[2][2] = 'c';
134         vbuf[3][3] = 'd';
135         
136 #if 0
137         region.start = 0xdeadbeef00000000ULL + kvirt_to_phys(abuf) + Arch::page_size;
138         region.end = region.start + Arch::page_size - 1;
139         printf("unmapping %llx->%llx\n", region.start, region.end);
140
141         stacked[0].unmap(region);
142 #endif
143
144         printf("%c%c%c%c ", vbuf[0][0], vbuf[0][1], vbuf[0][2], vbuf[0][3]);
145         printf("%c%c%c%c ", vbuf[1][0], vbuf[1][1], vbuf[1][2], vbuf[1][3]);
146         printf("%c%c%c%c ", vbuf[2][0], vbuf[2][1], vbuf[2][2], vbuf[2][3]);
147         printf("%c%c%c%c\n", vbuf[3][0], vbuf[3][1], vbuf[3][2], vbuf[3][3]);
148 }
149
150 void run_test()
151 {
152         Threads::sched.new_thread(thread, NULL, "thread")->wake();
153 }