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>
20 #include <kern/process.h>
24 extern IMappable physmem;
27 void thread(void *arg1, void *arg2)
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).
35 proc_addr_space_factory.create(&obj);
36 IAddrSpace aspace = IAddrSpace::downcast(obj);
38 printf("aspace %p created\n", (void *)aspace);
40 ProcAddrSpace *kaspace = static_cast<ProcAddrSpace *>(AddrSpace::classptr(aspace));
42 printf("kaspace %p\n", kaspace);
44 curthread->set_aspace(kaspace);
45 printf("Addr space set\n");
47 IAddrSpace stacked[2];
48 IMappable mappables[2];
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]);
56 printf("stacked %p %p mappable %p %p\n",
57 (void *)stacked[0], (void *)stacked[1],
58 (void *)mappables[0], (void *)mappables[1]);
60 // char *buf = new char[100000];
61 char *buf = (char *)alloc_pages(25);
62 printf("buf %p\n", buf);
64 char *abuf = (char *)((((ulong)buf) + 4095) & ~4095);
65 printf("abuf %p\n", abuf);
67 memset(abuf, 'A', Arch::page_size);
68 memset(abuf + Arch::page_size, 'B', Arch::page_size);
72 System::Mem::MapFlags mf;
73 mf.access_IDLNS_Read = 1;
74 mf.access_IDLNS_Write = 1;
78 region.end = 0xffffffff;
79 vstart[4] = 0xdeadbeef00000000ULL;
80 vstart[5] = 0xdeadb12345678000ULL;
82 stacked[0].map(Mem::physmem, region, &vstart[4], mf);
84 stacked[0].map(Mem::physmem, region, &vstart[5], mf);
86 printf("stacked[0] vstart %llx, %llx\n", vstart[4], vstart[5]);
88 region.start = 0xdeadb00000000000ULL;
89 region.end = 0xdeadbfffffffffffULL;
90 vstart[4] = 0x1234500000000000ULL;
92 // mf.access_IDLNS_Write = 0;
94 stacked[1].map(mappables[0], region, &vstart[4], mf);
96 printf("stacked[1] vstart %llx\n", vstart[4]);
98 region.start = vstart[4] + 0xeef00000000ULL + kvirt_to_phys(abuf);
99 region.end = vstart[4] + 0xeef00000000ULL + kvirt_to_phys(abuf) + Arch::page_size - 1;
100 vstart[0] = System::Mem::AddrSpace_ns::unspecified_start;
101 vstart[1] = System::Mem::AddrSpace_ns::unspecified_start;
102 vstart[2] = System::Mem::AddrSpace_ns::unspecified_start;
103 mf.access_IDLNS_Write = 1;
105 aspace.map(mappables[1], region, &vstart[0], mf);
107 region.start += Arch::page_size;
108 region.end += Arch::page_size;
109 aspace.map(mappables[1], region, &vstart[1], mf);
111 region.start = vstart[4] + 0x12345678000ULL + kvirt_to_phys(abuf);
112 region.end = vstart[4] + 0x12345678000ULL + kvirt_to_phys(abuf) + Arch::page_size - 1;
114 aspace.map(mappables[1], region, &vstart[2], mf);
117 aspace.map(mappables[1], region, &vstart[3], mf);
119 printf("vstart %llx %llx %llx %llx\n", vstart[0], vstart[1], vstart[2], vstart[3]);
121 vbuf[0] = (char *)vstart[0];
122 vbuf[1] = (char *)vstart[1];
123 vbuf[2] = (char *)vstart[2];
124 vbuf[3] = (char *)vstart[3];
126 printf("%c%c%c%c ", vbuf[0][0], vbuf[0][1], vbuf[0][2], vbuf[0][3]);
127 printf("%c%c%c%c ", vbuf[1][0], vbuf[1][1], vbuf[1][2], vbuf[1][3]);
128 printf("%c%c%c%c ", vbuf[2][0], vbuf[2][1], vbuf[2][2], vbuf[2][3]);
129 printf("%c%c%c%c\n", vbuf[3][0], vbuf[3][1], vbuf[3][2], vbuf[3][3]);
137 region.start = 0xdeadbeef00000000ULL + kvirt_to_phys(abuf) + Arch::page_size;
138 region.end = region.start + Arch::page_size - 1;
139 printf("unmapping %llx->%llx\n", region.start, region.end);
141 stacked[0].unmap(region);
144 printf("%c%c%c%c ", vbuf[0][0], vbuf[0][1], vbuf[0][2], vbuf[0][3]);
145 printf("%c%c%c%c ", vbuf[1][0], vbuf[1][1], vbuf[1][2], vbuf[1][3]);
146 printf("%c%c%c%c ", vbuf[2][0], vbuf[2][1], vbuf[2][2], vbuf[2][3]);
147 printf("%c%c%c%c\n", vbuf[3][0], vbuf[3][1], vbuf[3][2], vbuf[3][3]);
152 Threads::sched.new_thread(thread, NULL, NULL, "thread")->wake();