[142] | 1 | /* $Id: memory.cpp 152 2000-07-17 18:37:33Z sandervl $ */
|
---|
| 2 |
|
---|
| 3 | //******************************************************************************
|
---|
| 4 | // OS/2 implementation of Linux memory kernel services
|
---|
| 5 | //
|
---|
| 6 | // Copyright 2000 Sander van Leeuwen (sandervl@xs4all.nl)
|
---|
| 7 | //
|
---|
| 8 | // This program is free software; you can redistribute it and/or
|
---|
| 9 | // modify it under the terms of the GNU General Public License as
|
---|
| 10 | // published by the Free Software Foundation; either version 2 of
|
---|
| 11 | // the License, or (at your option) any later version.
|
---|
| 12 | //
|
---|
| 13 | // This program is distributed in the hope that it will be useful,
|
---|
| 14 | // but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 15 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 16 | // GNU General Public License for more details.
|
---|
| 17 | //
|
---|
| 18 | // You should have received a copy of the GNU General Public
|
---|
| 19 | // License along with this program; if not, write to the Free
|
---|
| 20 | // Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139,
|
---|
| 21 | // USA.
|
---|
| 22 | //
|
---|
| 23 | //******************************************************************************
|
---|
| 24 | extern "C" {
|
---|
| 25 | #define INCL_NOPMAPI
|
---|
| 26 | #define INCL_DOSERRORS // for ERROR_INVALID_FUNCTION
|
---|
| 27 | #include <os2.h>
|
---|
| 28 | }
|
---|
| 29 | #include <devhelp.h>
|
---|
| 30 | #include <ossidc.h>
|
---|
| 31 | #include <string.h>
|
---|
| 32 | #ifdef KEE
|
---|
| 33 | #include <kee.h>
|
---|
| 34 | #endif
|
---|
| 35 |
|
---|
| 36 | #pragma off (unreferenced)
|
---|
| 37 |
|
---|
| 38 | #define PAGE_SIZE 4096
|
---|
| 39 |
|
---|
| 40 | extern "C" {
|
---|
| 41 |
|
---|
| 42 | //******************************************************************************
|
---|
| 43 | //NOTE: Assumes memory is continuous!!
|
---|
| 44 | //******************************************************************************
|
---|
| 45 | unsigned long virt_to_phys(void * address)
|
---|
| 46 | {
|
---|
| 47 | #ifdef KEE
|
---|
| 48 | KEEVMPageList pagelist;
|
---|
| 49 | ULONG nrpages;
|
---|
| 50 |
|
---|
| 51 | if(KernLinToPageList(address, PAGE_SIZE, &pagelist, &nrpages)) {
|
---|
[148] | 52 | DebugInt3();
|
---|
[142] | 53 | return 0;
|
---|
| 54 | }
|
---|
| 55 | return pagelist.addr;
|
---|
| 56 | #else
|
---|
| 57 | LINEAR addr = (LINEAR)address;
|
---|
| 58 | PAGELIST pagelist;
|
---|
| 59 |
|
---|
| 60 | if(DevLinToPageList(addr, PAGE_SIZE, (PAGELIST near *)__StackToFlat((ULONG)&pagelist))) {
|
---|
[148] | 61 | DebugInt3();
|
---|
[142] | 62 | return 0;
|
---|
| 63 | }
|
---|
| 64 | return pagelist.physaddr;
|
---|
| 65 | #endif
|
---|
| 66 | }
|
---|
| 67 | //******************************************************************************
|
---|
| 68 | //******************************************************************************
|
---|
| 69 | void * phys_to_virt(unsigned long address)
|
---|
| 70 | {
|
---|
| 71 | DebugInt3();
|
---|
| 72 | return 0;
|
---|
| 73 | }
|
---|
| 74 | //******************************************************************************
|
---|
| 75 | //******************************************************************************
|
---|
| 76 | void *__get_free_pages(int gfp_mask, unsigned long order)
|
---|
| 77 | {
|
---|
| 78 | ULONG addr;
|
---|
| 79 |
|
---|
| 80 | order = (1 << order); //TODO: Is this correct???
|
---|
| 81 | #ifdef KEE
|
---|
| 82 | SHORT sel;
|
---|
| 83 |
|
---|
| 84 | if(KernVMAlloc(order*PAGE_SIZE, VMDHA_FIXED|VMDHA_CONTIG,
|
---|
| 85 | (PVOID*)&addr, (PVOID*)-1, &sel)) {
|
---|
| 86 | #else
|
---|
| 87 | if(DevVMAlloc(VMDHA_FIXED|VMDHA_CONTIG, order*PAGE_SIZE, (LINEAR)-1, __StackToFlat((ULONG)&addr))) {
|
---|
| 88 | #endif
|
---|
[148] | 89 | DebugInt3();
|
---|
[142] | 90 | return 0;
|
---|
| 91 | }
|
---|
[148] | 92 | //// dprintf(("__get_free_pages %d returned %x", order*PAGE_SIZE, addr));
|
---|
[142] | 93 | return (void *)addr;
|
---|
| 94 | }
|
---|
| 95 | //******************************************************************************
|
---|
| 96 | //******************************************************************************
|
---|
| 97 | int free_pages(unsigned long addr, unsigned long order)
|
---|
| 98 | {
|
---|
| 99 | #ifdef KEE
|
---|
| 100 | KernVMFree((PVOID)addr);
|
---|
| 101 | #else
|
---|
| 102 | DevVMFree((LINEAR)addr);
|
---|
| 103 | #endif
|
---|
[148] | 104 | //// dprintf(("free_pages %x", addr));
|
---|
[142] | 105 | return 0;
|
---|
| 106 | }
|
---|
| 107 | //******************************************************************************
|
---|
| 108 | //******************************************************************************
|
---|
| 109 | struct page * alloc_pages(int gfp_mask, unsigned long order)
|
---|
| 110 | {
|
---|
| 111 | DebugInt3();
|
---|
| 112 | return 0;
|
---|
| 113 | }
|
---|
| 114 | //******************************************************************************
|
---|
| 115 | //******************************************************************************
|
---|
| 116 | int remap_page_range(unsigned long from, unsigned long to, unsigned long size, unsigned long prot)
|
---|
| 117 | {
|
---|
| 118 | DebugInt3();
|
---|
| 119 | return 0;
|
---|
| 120 | }
|
---|
| 121 | //******************************************************************************
|
---|
| 122 | //******************************************************************************
|
---|
| 123 | int is_access_ok(int type, void *addr, unsigned long size)
|
---|
| 124 | {
|
---|
| 125 | return 1;
|
---|
| 126 | }
|
---|
| 127 | //******************************************************************************
|
---|
| 128 | //******************************************************************************
|
---|
| 129 | void __copy_user(void *to, const void *from, unsigned long n)
|
---|
| 130 | {
|
---|
[152] | 131 | if(to == NULL || from == NULL) {
|
---|
| 132 | DebugInt3();
|
---|
| 133 | return;
|
---|
| 134 | }
|
---|
| 135 | if(n == 0) return;
|
---|
[142] | 136 | #ifdef KEE
|
---|
| 137 | memcpy(to, from, n);
|
---|
| 138 | #else
|
---|
| 139 | _fmemcpy(to, from, n);
|
---|
| 140 | #endif
|
---|
| 141 | }
|
---|
| 142 | //******************************************************************************
|
---|
| 143 | //******************************************************************************
|
---|
| 144 | unsigned long copy_to_user(void *to, const void *from, unsigned long n)
|
---|
| 145 | {
|
---|
[152] | 146 | if(to == NULL || from == NULL) {
|
---|
| 147 | DebugInt3();
|
---|
| 148 | return 0;
|
---|
| 149 | }
|
---|
| 150 | if(n == 0) return 0;
|
---|
[142] | 151 | #ifdef KEE
|
---|
| 152 | memcpy(to, from, n);
|
---|
| 153 | #else
|
---|
| 154 | _fmemcpy(to, from, n);
|
---|
| 155 | #endif
|
---|
| 156 | return 0;
|
---|
| 157 | }
|
---|
| 158 | //******************************************************************************
|
---|
| 159 | //******************************************************************************
|
---|
| 160 | void __copy_user_zeroing(void *to, const void *from, unsigned long n)
|
---|
| 161 | {
|
---|
[152] | 162 | if(to == NULL || from == NULL) {
|
---|
| 163 | DebugInt3();
|
---|
| 164 | return;
|
---|
| 165 | }
|
---|
| 166 | if(n == 0) return;
|
---|
[142] | 167 | copy_to_user(to, from, n);
|
---|
| 168 | }
|
---|
| 169 | //******************************************************************************
|
---|
| 170 | //******************************************************************************
|
---|
| 171 | unsigned long copy_from_user(void *to, const void *from, unsigned long n)
|
---|
| 172 | {
|
---|
[152] | 173 | if(to == NULL || from == NULL) {
|
---|
| 174 | DebugInt3();
|
---|
| 175 | return 0;
|
---|
| 176 | }
|
---|
| 177 | if(n == 0) return 0;
|
---|
[142] | 178 | #ifdef KEE
|
---|
| 179 | memcpy(to, from, n);
|
---|
| 180 | #else
|
---|
| 181 | _fmemcpy(to, from, n);
|
---|
| 182 | #endif
|
---|
| 183 | return 0;
|
---|
| 184 | }
|
---|
| 185 | //******************************************************************************
|
---|
| 186 | //******************************************************************************
|
---|
| 187 | int get_user(int size, void *dest, void *src)
|
---|
| 188 | {
|
---|
[152] | 189 | if(size == 0) return 0;
|
---|
| 190 |
|
---|
| 191 | if(dest == NULL || src == NULL) {
|
---|
| 192 | DebugInt3();
|
---|
| 193 | return 0;
|
---|
| 194 | }
|
---|
[142] | 195 | #ifdef KEE
|
---|
| 196 | memcpy(dest, src, size);
|
---|
| 197 | #else
|
---|
| 198 | _fmemcpy(dest, src, size);
|
---|
| 199 | #endif
|
---|
| 200 | return 0;
|
---|
| 201 | }
|
---|
| 202 | //******************************************************************************
|
---|
| 203 | //******************************************************************************
|
---|
| 204 | int put_user(int x, void *ptr)
|
---|
| 205 | {
|
---|
[152] | 206 | if(ptr == NULL) {
|
---|
| 207 | DebugInt3();
|
---|
| 208 | return 0;
|
---|
| 209 | }
|
---|
| 210 |
|
---|
[142] | 211 | *(int *)ptr = x;
|
---|
| 212 | return 0;
|
---|
| 213 | }
|
---|
| 214 | //******************************************************************************
|
---|
| 215 | //******************************************************************************
|
---|
| 216 | #if 0
|
---|
| 217 | int __put_user(int size, int x, void *ptr)
|
---|
| 218 | {
|
---|
| 219 | #ifdef KEE
|
---|
| 220 | memcpy(ptr,
|
---|
| 221 | #else
|
---|
| 222 | _fmemcpy(ptr,
|
---|
| 223 | #endif
|
---|
| 224 | return 0;
|
---|
| 225 | }
|
---|
| 226 | #endif
|
---|
| 227 | //******************************************************************************
|
---|
| 228 | //******************************************************************************
|
---|
| 229 | void *kmalloc(int size, int flags)
|
---|
| 230 | {
|
---|
| 231 | char near *addr;
|
---|
| 232 |
|
---|
[152] | 233 | if(size == 0) {
|
---|
| 234 | DebugInt3();
|
---|
| 235 | return NULL;
|
---|
| 236 | }
|
---|
[142] | 237 | if(size > 1024) {
|
---|
| 238 | #ifdef KEE
|
---|
| 239 | SHORT sel;
|
---|
| 240 |
|
---|
| 241 | if(KernVMAlloc(size+4, VMDHA_FIXED,
|
---|
| 242 | (PVOID*)&addr, (PVOID*)-1, &sel)) {
|
---|
| 243 | #else
|
---|
| 244 | if(DevVMAlloc(VMDHA_FIXED, size+4, (LINEAR)-1, __StackToFlat((ULONG)&addr))) {
|
---|
| 245 | #endif
|
---|
[148] | 246 | DebugInt3();
|
---|
[142] | 247 | return 0;
|
---|
| 248 | }
|
---|
| 249 | *(ULONG *)addr = 0; //flat address
|
---|
[148] | 250 | //// dprintf(("kmalloc %d returned %x", size, addr));
|
---|
[142] | 251 | return addr+4;
|
---|
| 252 | }
|
---|
| 253 | addr = (char near *)CallOSS16(IDC16_MALLOC, size, 0);
|
---|
| 254 | if(addr == NULL) {
|
---|
| 255 | DebugInt3();
|
---|
| 256 | return 0;
|
---|
| 257 | }
|
---|
[148] | 258 | //// dprintf(("kmalloc %d returned %x", size, addr));
|
---|
[142] | 259 | return addr+4; //first 4 bytes contain original 16:16 address
|
---|
| 260 | }
|
---|
| 261 | //******************************************************************************
|
---|
| 262 | //******************************************************************************
|
---|
| 263 | void kfree(const void *ptr)
|
---|
| 264 | {
|
---|
| 265 | ULONG addr;
|
---|
| 266 |
|
---|
| 267 | addr = (ULONG)ptr;
|
---|
| 268 | if(addr == NULL) {
|
---|
| 269 | DebugInt3();
|
---|
| 270 | return;
|
---|
| 271 | }
|
---|
| 272 | addr -= 4; //first 4 bytes contain original 16:16 address or 0 if allocated by VMAlloc
|
---|
[148] | 273 | //// dprintf(("kfree %x", addr));
|
---|
[142] | 274 | if(*(ULONG near *)addr) {
|
---|
| 275 | CallOSS16(IDC16_FREE, *(ULONG near *)addr, 0);
|
---|
| 276 | }
|
---|
| 277 | #ifdef KEE
|
---|
| 278 | else KernVMFree((PVOID)addr);
|
---|
| 279 | #else
|
---|
| 280 | else DevVMFree((LINEAR)addr);
|
---|
| 281 | #endif
|
---|
| 282 | }
|
---|
| 283 | //******************************************************************************
|
---|
| 284 | //******************************************************************************
|
---|
| 285 | void kfree_s(const void *ptr, unsigned int size)
|
---|
| 286 | {
|
---|
| 287 | kfree(ptr);
|
---|
| 288 | }
|
---|
| 289 | //******************************************************************************
|
---|
| 290 | //******************************************************************************
|
---|
| 291 |
|
---|
| 292 | }
|
---|