3 // This software is copyright (c) 2006 Scott Wood <scott@buserror.net>.
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:
12 // * Redistributions of source code must retain the above copyright notice,
13 // this list of conditions and the following disclaimers.
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.
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.
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
31 #include <kern/types.h>
32 #include <kern/libc.h>
33 #include <kern/time.h>
34 #include <kern/thread.h>
39 extern IMappable physmem;
42 void thread(void *arg)
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).
50 proc_addr_space_factory.create(&obj);
51 IAddrSpace aspace = IAddrSpace::downcast(obj);
53 printf("aspace %p created\n", (void *)aspace);
55 AddrSpace *kaspace = AddrSpace::classptr(aspace);
57 printf("kaspace %p\n", kaspace);
59 curthread->set_aspace(kaspace);
60 printf("Addr space set\n");
62 IAddrSpace stacked[2];
63 IMappable mappables[2];
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]);
71 printf("stacked %p %p mappable %p %p\n",
72 (void *)stacked[0], (void *)stacked[1],
73 (void *)mappables[0], (void *)mappables[1]);
75 char *buf = new char[100000];
76 printf("buf %p\n", buf);
78 char *abuf = (char *)((((ulong)buf) + 4095) & ~4095);
79 printf("abuf %p\n", abuf);
81 memset(abuf, 'A', Arch::page_size);
82 memset(abuf + Arch::page_size, 'B', Arch::page_size);
86 System::Mem::MapFlags mf;
87 mf.access_IDLNS_Read = 1;
88 mf.access_IDLNS_Write = 1;
92 region.end = 0xffffffff;
93 vstart[4] = 0xdeadbeef00000000ULL;
94 vstart[5] = 0xdeadb12345678000ULL;
96 stacked[0].map(Mem::physmem, region, &vstart[4], mf);
98 stacked[0].map(Mem::physmem, region, &vstart[5], mf);
100 printf("stacked[0] vstart %llx, %llx\n", vstart[4], vstart[5]);
102 region.start = 0xdeadb00000000000ULL;
103 region.end = 0xdeadbfffffffffffULL;
104 vstart[4] = 0x1234500000000000ULL;
106 // mf.access_IDLNS_Write = 0;
108 stacked[1].map(mappables[0], region, &vstart[4], mf);
110 printf("stacked[1] vstart %llx\n", vstart[4]);
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;
119 aspace.map(mappables[1], region, &vstart[0], mf);
121 region.start += Arch::page_size;
122 region.end += Arch::page_size;
123 aspace.map(mappables[1], region, &vstart[1], mf);
125 region.start = vstart[4] + 0x12345678000ULL + kvirt_to_phys(abuf);
126 region.end = vstart[4] + 0x12345678000ULL + kvirt_to_phys(abuf) + Arch::page_size - 1;
128 aspace.map(mappables[1], region, &vstart[2], mf);
131 aspace.map(mappables[1], region, &vstart[3], mf);
133 printf("vstart %llx %llx %llx %llx\n", vstart[0], vstart[1], vstart[2], vstart[3]);
135 vbuf[0] = (char *)vstart[0];
136 vbuf[1] = (char *)vstart[1];
137 vbuf[2] = (char *)vstart[2];
138 vbuf[3] = (char *)vstart[3];
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]);
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);
155 stacked[0].unmap(region);
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]);
166 Threads::sched.new_thread(thread, NULL, "thread")->wake();