3 // This software is copyright (c) 2006 Scott Wood <scott@buserror.net>.
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.
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.
15 #include <kern/types.h>
16 #include <kern/libc.h>
17 #include <kern/time.h>
18 #include <kern/thread.h>
23 extern IMappable physmem;
26 void thread(void *arg)
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).
34 proc_addr_space_factory.create(&obj);
35 IAddrSpace aspace = IAddrSpace::downcast(obj);
37 printf("aspace %p created\n", (void *)aspace);
39 ProcAddrSpace *kaspace = static_cast<ProcAddrSpace *>(AddrSpace::classptr(aspace));
41 printf("kaspace %p\n", kaspace);
43 curthread->set_aspace(kaspace);
44 printf("Addr space set\n");
46 IAddrSpace stacked[2];
47 IMappable mappables[2];
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]);
55 printf("stacked %p %p mappable %p %p\n",
56 (void *)stacked[0], (void *)stacked[1],
57 (void *)mappables[0], (void *)mappables[1]);
59 char *buf = new char[100000];
60 printf("buf %p\n", buf);
62 char *abuf = (char *)((((ulong)buf) + 4095) & ~4095);
63 printf("abuf %p\n", abuf);
65 memset(abuf, 'A', Arch::page_size);
66 memset(abuf + Arch::page_size, 'B', Arch::page_size);
70 System::Mem::MapFlags mf;
71 mf.access_IDLNS_Read = 1;
72 mf.access_IDLNS_Write = 1;
76 region.end = 0xffffffff;
77 vstart[4] = 0xdeadbeef00000000ULL;
78 vstart[5] = 0xdeadb12345678000ULL;
80 stacked[0].map(Mem::physmem, region, &vstart[4], mf);
82 stacked[0].map(Mem::physmem, region, &vstart[5], mf);
84 printf("stacked[0] vstart %llx, %llx\n", vstart[4], vstart[5]);
86 region.start = 0xdeadb00000000000ULL;
87 region.end = 0xdeadbfffffffffffULL;
88 vstart[4] = 0x1234500000000000ULL;
90 // mf.access_IDLNS_Write = 0;
92 stacked[1].map(mappables[0], region, &vstart[4], mf);
94 printf("stacked[1] vstart %llx\n", vstart[4]);
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;
103 aspace.map(mappables[1], region, &vstart[0], mf);
105 region.start += Arch::page_size;
106 region.end += Arch::page_size;
107 aspace.map(mappables[1], region, &vstart[1], mf);
109 region.start = vstart[4] + 0x12345678000ULL + kvirt_to_phys(abuf);
110 region.end = vstart[4] + 0x12345678000ULL + kvirt_to_phys(abuf) + Arch::page_size - 1;
112 aspace.map(mappables[1], region, &vstart[2], mf);
115 aspace.map(mappables[1], region, &vstart[3], mf);
117 printf("vstart %llx %llx %llx %llx\n", vstart[0], vstart[1], vstart[2], vstart[3]);
119 vbuf[0] = (char *)vstart[0];
120 vbuf[1] = (char *)vstart[1];
121 vbuf[2] = (char *)vstart[2];
122 vbuf[3] = (char *)vstart[3];
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]);
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);
139 stacked[0].unmap(region);
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]);
150 Threads::sched.new_thread(thread, NULL, "thread")->wake();