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