]> git.buserror.net Git - polintos/scott/priv.git/blob - kernel/tests/aspace.cc
Switch to a simple X11-style license.
[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 // Permission is hereby granted, free of charge, to any person obtaining a copy of
6 // this software and associated documentation files (the "Software"), to deal with
7 // the Software without restriction, including without limitation the rights to
8 // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
9 // of the Software, and to permit persons to whom the Software is furnished to do
10 // so, subject to the following condition:
11 // 
12 // The above copyright notice and this permission notice shall be
13 // included in all copies or substantial portions of the Software.
14 // 
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17 // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
18 // CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE
21 // SOFTWARE.
22
23 #include <kern/types.h>
24 #include <kern/libc.h>
25 #include <kern/time.h>
26 #include <kern/thread.h>
27 #include <kern/mem.h>
28
29 using namespace Mem;
30 namespace Mem {
31         extern IMappable physmem;
32 }
33
34 void thread(void *arg)
35 {
36         // Yuck -- out param->return value promotion would turn it into
37         // AddrSpace aspace = AddrSpace::downcast(Mem::proc_addr_space_factory.create()),
38         // but that's still ugly.  Perhaps something like:
39         // AddrSpace aspace(Mem::proc_addr_space_factory).
40         
41         Object obj;
42         proc_addr_space_factory.create(&obj);
43         IAddrSpace aspace = IAddrSpace::downcast(obj);
44         
45         printf("aspace %p created\n", (void *)aspace);
46         
47         AddrSpace *kaspace = AddrSpace::classptr(aspace);
48         
49         printf("kaspace %p\n", kaspace);
50         
51         curthread->set_aspace(kaspace);
52         printf("Addr space set\n");
53
54         IAddrSpace stacked[2];
55         IMappable mappables[2];
56
57         for (int i = 0; i < 2; i++) {
58                 addr_space_factory.create(&obj);
59                 stacked[i] = IAddrSpace::downcast(obj);
60                 stacked[i].get_mappable(&mappables[i]);
61         }
62         
63         printf("stacked %p %p mappable %p %p\n", 
64                (void *)stacked[0], (void *)stacked[1],
65                (void *)mappables[0], (void *)mappables[1]);
66                 
67         char *buf = new char[100000];
68         printf("buf %p\n", buf);
69         
70         char *abuf = (char *)((((ulong)buf) + 4095) & ~4095);
71         printf("abuf %p\n", abuf);
72         
73         memset(abuf, 'A', Arch::page_size);
74         memset(abuf + Arch::page_size, 'B', Arch::page_size);
75
76         Region region;
77         u64 vstart[6];
78         System::Mem::MapFlags mf;
79         mf.access_IDLNS_Read = 1;
80         mf.access_IDLNS_Write = 1;
81         mf.Fixed = 1;
82         
83         region.start = 0;
84         region.end = 0xffffffff;
85         vstart[4] = 0xdeadbeef00000000ULL;
86         vstart[5] = 0xdeadb12345678000ULL;
87         
88         stacked[0].map(Mem::physmem, region, &vstart[4], mf);
89         mf.CopyOnWrite = 1;
90         stacked[0].map(Mem::physmem, region, &vstart[5], mf);
91         mf.CopyOnWrite = 0;
92         printf("stacked[0] vstart %llx, %llx\n", vstart[4], vstart[5]);
93         
94         region.start = 0xdeadb00000000000ULL;
95         region.end   = 0xdeadbfffffffffffULL;
96         vstart[4]    = 0x1234500000000000ULL;
97         mf.Fixed = 0;
98 //      mf.access_IDLNS_Write = 0;
99         
100         stacked[1].map(mappables[0], region, &vstart[4], mf);
101
102         printf("stacked[1] vstart %llx\n", vstart[4]);
103         
104         region.start = vstart[4] + 0xeef00000000ULL + kvirt_to_phys(abuf);
105         region.end   = vstart[4] + 0xeef00000000ULL + kvirt_to_phys(abuf) + Arch::page_size - 1;
106         vstart[0] = System::Mem::AddrSpace_ns::unspecified_start;
107         vstart[1] = System::Mem::AddrSpace_ns::unspecified_start;
108         vstart[2] = System::Mem::AddrSpace_ns::unspecified_start;
109         mf.access_IDLNS_Write = 1;
110         
111         aspace.map(mappables[1], region, &vstart[0], mf);
112         
113         region.start += Arch::page_size;
114         region.end += Arch::page_size;
115         aspace.map(mappables[1], region, &vstart[1], mf);
116
117         region.start = vstart[4] + 0x12345678000ULL + kvirt_to_phys(abuf);
118         region.end   = vstart[4] + 0x12345678000ULL + kvirt_to_phys(abuf) + Arch::page_size - 1;
119
120         aspace.map(mappables[1], region, &vstart[2], mf);
121
122         mf.CopyOnWrite = 1;
123         aspace.map(mappables[1], region, &vstart[3], mf);
124
125         printf("vstart %llx %llx %llx %llx\n", vstart[0], vstart[1], vstart[2], vstart[3]);
126         char *vbuf[4];
127         vbuf[0] = (char *)vstart[0];
128         vbuf[1] = (char *)vstart[1];
129         vbuf[2] = (char *)vstart[2];
130         vbuf[3] = (char *)vstart[3];
131
132         printf("%c%c%c%c ", vbuf[0][0], vbuf[0][1], vbuf[0][2], vbuf[0][3]);
133         printf("%c%c%c%c ", vbuf[1][0], vbuf[1][1], vbuf[1][2], vbuf[1][3]);
134         printf("%c%c%c%c ", vbuf[2][0], vbuf[2][1], vbuf[2][2], vbuf[2][3]);
135         printf("%c%c%c%c\n", vbuf[3][0], vbuf[3][1], vbuf[3][2], vbuf[3][3]);
136         
137         vbuf[0][0] = 'a';
138         vbuf[1][1] = 'b';
139         vbuf[2][2] = 'c';
140         vbuf[3][3] = 'd';
141         
142 #if 0
143         region.start = 0xdeadbeef00000000ULL + kvirt_to_phys(abuf) + Arch::page_size;
144         region.end = region.start + Arch::page_size - 1;
145         printf("unmapping %llx->%llx\n", region.start, region.end);
146
147         stacked[0].unmap(region);
148 #endif
149
150         printf("%c%c%c%c ", vbuf[0][0], vbuf[0][1], vbuf[0][2], vbuf[0][3]);
151         printf("%c%c%c%c ", vbuf[1][0], vbuf[1][1], vbuf[1][2], vbuf[1][3]);
152         printf("%c%c%c%c ", vbuf[2][0], vbuf[2][1], vbuf[2][2], vbuf[2][3]);
153         printf("%c%c%c%c\n", vbuf[3][0], vbuf[3][1], vbuf[3][2], vbuf[3][3]);
154 }
155
156 void run_test()
157 {
158         Threads::sched.new_thread(thread, NULL, "thread")->wake();
159 }