]> git.buserror.net Git - polintos/scott/priv.git/blob - kernel/tests/aspace.cc
694021ebc507dd16c7bab594d3dc1a1c2cebb902
[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         printf("buf %p\n", buf);
62         
63         char *abuf = (char *)((((ulong)buf) + 4095) & ~4095);
64         printf("abuf %p\n", abuf);
65         
66         memset(abuf, 'A', Arch::page_size);
67         memset(abuf + Arch::page_size, 'B', Arch::page_size);
68
69         Region region;
70         u64 vstart[6];
71         System::Mem::MapFlags mf;
72         mf.access_IDLNS_Read = 1;
73         mf.access_IDLNS_Write = 1;
74         mf.Fixed = 1;
75         
76         region.start = 0;
77         region.end = 0xffffffff;
78         vstart[4] = 0xdeadbeef00000000ULL;
79         vstart[5] = 0xdeadb12345678000ULL;
80         
81         stacked[0].map(Mem::physmem, region, &vstart[4], mf);
82         mf.CopyOnWrite = 1;
83         stacked[0].map(Mem::physmem, region, &vstart[5], mf);
84         mf.CopyOnWrite = 0;
85         printf("stacked[0] vstart %llx, %llx\n", vstart[4], vstart[5]);
86         
87         region.start = 0xdeadb00000000000ULL;
88         region.end   = 0xdeadbfffffffffffULL;
89         vstart[4]    = 0x1234500000000000ULL;
90         mf.Fixed = 0;
91 //      mf.access_IDLNS_Write = 0;
92         
93         stacked[1].map(mappables[0], region, &vstart[4], mf);
94
95         printf("stacked[1] vstart %llx\n", vstart[4]);
96         
97         region.start = vstart[4] + 0xeef00000000ULL + kvirt_to_phys(abuf);
98         region.end   = vstart[4] + 0xeef00000000ULL + kvirt_to_phys(abuf) + Arch::page_size - 1;
99         vstart[0] = System::Mem::AddrSpace_ns::unspecified_start;
100         vstart[1] = System::Mem::AddrSpace_ns::unspecified_start;
101         vstart[2] = System::Mem::AddrSpace_ns::unspecified_start;
102         mf.access_IDLNS_Write = 1;
103         
104         aspace.map(mappables[1], region, &vstart[0], mf);
105         
106         region.start += Arch::page_size;
107         region.end += Arch::page_size;
108         aspace.map(mappables[1], region, &vstart[1], mf);
109
110         region.start = vstart[4] + 0x12345678000ULL + kvirt_to_phys(abuf);
111         region.end   = vstart[4] + 0x12345678000ULL + kvirt_to_phys(abuf) + Arch::page_size - 1;
112
113         aspace.map(mappables[1], region, &vstart[2], mf);
114
115         mf.CopyOnWrite = 1;
116         aspace.map(mappables[1], region, &vstart[3], mf);
117
118         printf("vstart %llx %llx %llx %llx\n", vstart[0], vstart[1], vstart[2], vstart[3]);
119         char *vbuf[4];
120         vbuf[0] = (char *)vstart[0];
121         vbuf[1] = (char *)vstart[1];
122         vbuf[2] = (char *)vstart[2];
123         vbuf[3] = (char *)vstart[3];
124
125         printf("%c%c%c%c ", vbuf[0][0], vbuf[0][1], vbuf[0][2], vbuf[0][3]);
126         printf("%c%c%c%c ", vbuf[1][0], vbuf[1][1], vbuf[1][2], vbuf[1][3]);
127         printf("%c%c%c%c ", vbuf[2][0], vbuf[2][1], vbuf[2][2], vbuf[2][3]);
128         printf("%c%c%c%c\n", vbuf[3][0], vbuf[3][1], vbuf[3][2], vbuf[3][3]);
129         
130         vbuf[0][0] = 'a';
131         vbuf[1][1] = 'b';
132         vbuf[2][2] = 'c';
133         vbuf[3][3] = 'd';
134         
135 #if 0
136         region.start = 0xdeadbeef00000000ULL + kvirt_to_phys(abuf) + Arch::page_size;
137         region.end = region.start + Arch::page_size - 1;
138         printf("unmapping %llx->%llx\n", region.start, region.end);
139
140         stacked[0].unmap(region);
141 #endif
142
143         printf("%c%c%c%c ", vbuf[0][0], vbuf[0][1], vbuf[0][2], vbuf[0][3]);
144         printf("%c%c%c%c ", vbuf[1][0], vbuf[1][1], vbuf[1][2], vbuf[1][3]);
145         printf("%c%c%c%c ", vbuf[2][0], vbuf[2][1], vbuf[2][2], vbuf[2][3]);
146         printf("%c%c%c%c\n", vbuf[3][0], vbuf[3][1], vbuf[3][2], vbuf[3][3]);
147 }
148
149 void run_test()
150 {
151         Threads::sched.new_thread(thread, NULL, "thread")->wake();
152 }