]> git.buserror.net Git - polintos/scott/priv.git/blob - kernel/tests/aspace.cc
18f9d05a4d751716a505936980c79d54ff6c1f48
[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
21 using namespace Mem;
22 namespace Mem {
23         extern IMappable physmem;
24 }
25
26 void thread(void *arg)
27 {
28         // Yuck -- out param->return value promotion would turn it into
29         // AddrSpace aspace = AddrSpace::downcast(Mem::proc_addr_space_factory.create()),
30         // but that's still ugly.  Perhaps something like:
31         // AddrSpace aspace(Mem::proc_addr_space_factory).
32         
33         Object obj;
34         proc_addr_space_factory.create(&obj);
35         IAddrSpace aspace = IAddrSpace::downcast(obj);
36         
37         printf("aspace %p created\n", (void *)aspace);
38         
39         AddrSpace *kaspace = AddrSpace::classptr(aspace);
40         
41         printf("kaspace %p\n", kaspace);
42         
43         curthread->set_aspace(kaspace);
44         printf("Addr space set\n");
45
46         IAddrSpace stacked[2];
47         IMappable mappables[2];
48
49         for (int i = 0; i < 2; i++) {
50                 addr_space_factory.create(&obj);
51                 stacked[i] = IAddrSpace::downcast(obj);
52                 stacked[i].get_mappable(&mappables[i]);
53         }
54         
55         printf("stacked %p %p mappable %p %p\n", 
56                (void *)stacked[0], (void *)stacked[1],
57                (void *)mappables[0], (void *)mappables[1]);
58                 
59         char *buf = new char[100000];
60         printf("buf %p\n", buf);
61         
62         char *abuf = (char *)((((ulong)buf) + 4095) & ~4095);
63         printf("abuf %p\n", abuf);
64         
65         memset(abuf, 'A', Arch::page_size);
66         memset(abuf + Arch::page_size, 'B', Arch::page_size);
67
68         Region region;
69         u64 vstart[6];
70         System::Mem::MapFlags mf;
71         mf.access_IDLNS_Read = 1;
72         mf.access_IDLNS_Write = 1;
73         mf.Fixed = 1;
74         
75         region.start = 0;
76         region.end = 0xffffffff;
77         vstart[4] = 0xdeadbeef00000000ULL;
78         vstart[5] = 0xdeadb12345678000ULL;
79         
80         stacked[0].map(Mem::physmem, region, &vstart[4], mf);
81         mf.CopyOnWrite = 1;
82         stacked[0].map(Mem::physmem, region, &vstart[5], mf);
83         mf.CopyOnWrite = 0;
84         printf("stacked[0] vstart %llx, %llx\n", vstart[4], vstart[5]);
85         
86         region.start = 0xdeadb00000000000ULL;
87         region.end   = 0xdeadbfffffffffffULL;
88         vstart[4]    = 0x1234500000000000ULL;
89         mf.Fixed = 0;
90 //      mf.access_IDLNS_Write = 0;
91         
92         stacked[1].map(mappables[0], region, &vstart[4], mf);
93
94         printf("stacked[1] vstart %llx\n", vstart[4]);
95         
96         region.start = vstart[4] + 0xeef00000000ULL + kvirt_to_phys(abuf);
97         region.end   = vstart[4] + 0xeef00000000ULL + kvirt_to_phys(abuf) + Arch::page_size - 1;
98         vstart[0] = System::Mem::AddrSpace_ns::unspecified_start;
99         vstart[1] = System::Mem::AddrSpace_ns::unspecified_start;
100         vstart[2] = System::Mem::AddrSpace_ns::unspecified_start;
101         mf.access_IDLNS_Write = 1;
102         
103         aspace.map(mappables[1], region, &vstart[0], mf);
104         
105         region.start += Arch::page_size;
106         region.end += Arch::page_size;
107         aspace.map(mappables[1], region, &vstart[1], mf);
108
109         region.start = vstart[4] + 0x12345678000ULL + kvirt_to_phys(abuf);
110         region.end   = vstart[4] + 0x12345678000ULL + kvirt_to_phys(abuf) + Arch::page_size - 1;
111
112         aspace.map(mappables[1], region, &vstart[2], mf);
113
114         mf.CopyOnWrite = 1;
115         aspace.map(mappables[1], region, &vstart[3], mf);
116
117         printf("vstart %llx %llx %llx %llx\n", vstart[0], vstart[1], vstart[2], vstart[3]);
118         char *vbuf[4];
119         vbuf[0] = (char *)vstart[0];
120         vbuf[1] = (char *)vstart[1];
121         vbuf[2] = (char *)vstart[2];
122         vbuf[3] = (char *)vstart[3];
123
124         printf("%c%c%c%c ", vbuf[0][0], vbuf[0][1], vbuf[0][2], vbuf[0][3]);
125         printf("%c%c%c%c ", vbuf[1][0], vbuf[1][1], vbuf[1][2], vbuf[1][3]);
126         printf("%c%c%c%c ", vbuf[2][0], vbuf[2][1], vbuf[2][2], vbuf[2][3]);
127         printf("%c%c%c%c\n", vbuf[3][0], vbuf[3][1], vbuf[3][2], vbuf[3][3]);
128         
129         vbuf[0][0] = 'a';
130         vbuf[1][1] = 'b';
131         vbuf[2][2] = 'c';
132         vbuf[3][3] = 'd';
133         
134 #if 0
135         region.start = 0xdeadbeef00000000ULL + kvirt_to_phys(abuf) + Arch::page_size;
136         region.end = region.start + Arch::page_size - 1;
137         printf("unmapping %llx->%llx\n", region.start, region.end);
138
139         stacked[0].unmap(region);
140 #endif
141
142         printf("%c%c%c%c ", vbuf[0][0], vbuf[0][1], vbuf[0][2], vbuf[0][3]);
143         printf("%c%c%c%c ", vbuf[1][0], vbuf[1][1], vbuf[1][2], vbuf[1][3]);
144         printf("%c%c%c%c ", vbuf[2][0], vbuf[2][1], vbuf[2][2], vbuf[2][3]);
145         printf("%c%c%c%c\n", vbuf[3][0], vbuf[3][1], vbuf[3][2], vbuf[3][3]);
146 }
147
148 void run_test()
149 {
150         Threads::sched.new_thread(thread, NULL, "thread")->wake();
151 }