|
|
1.1.1.2 root 1: /*
2: * linux/mm/memory.c
3: *
4: * (C) 1991 Linus Torvalds
5: */
6:
1.1.1.3 root 7: /*
8: * demand-loading started 01.12.91 - seems it is high on the list of
9: * things wanted, and it should be easy to implement. - Linus
10: */
11:
12: /*
13: * Ok, demand-loading was easy, shared pages a little bit tricker. Shared
14: * pages started 02.12.91, seems to work. - Linus.
15: *
16: * Tested sharing by executing about 30 /bin/sh: under the old kernel it
17: * would have taken more than the 6M I have free, but it worked well as
18: * far as I could see.
19: *
20: * Also corrected some "invalidate()"s - I wasn't doing enough of them.
21: */
22:
1.1.1.4 root 23: /*
24: * Real VM (paging to/from disk) started 18.12.91. Much more work and
25: * thought has to go into this. Oh, well..
26: * 19.12.91 - works, somewhat. Sometimes I get faults, don't know why.
27: * Found it. Everything seems to work now.
28: * 20.12.91 - Ok, making the swap-device changeable like the root.
29: */
30:
1.1 root 31: #include <signal.h>
32:
1.1.1.3 root 33: #include <asm/system.h>
34:
35: #include <linux/sched.h>
1.1 root 36: #include <linux/head.h>
37: #include <linux/kernel.h>
38:
1.1.1.3 root 39: #define CODE_SPACE(addr) ((((addr)+4095)&~4095) < \
40: current->start_code + current->end_code)
41:
1.1.1.4 root 42: unsigned long HIGH_MEMORY = 0;
1.1 root 43:
44: #define copy_page(from,to) \
45: __asm__("cld ; rep ; movsl"::"S" (from),"D" (to),"c" (1024):"cx","di","si")
46:
1.1.1.5 root 47: #define CHECK_LAST_NR 16
48:
49: static unsigned long last_pages[CHECK_LAST_NR] = { 0, };
50:
1.1.1.4 root 51: unsigned char mem_map [ PAGING_PAGES ] = {0,};
1.1 root 52:
53: /*
54: * Free a page of memory at physical address 'addr'. Used by
55: * 'free_page_tables()'
56: */
57: void free_page(unsigned long addr)
58: {
1.1.1.2 root 59: if (addr < LOW_MEM) return;
1.1.1.5 root 60: if (addr < HIGH_MEMORY) {
61: addr -= LOW_MEM;
62: addr >>= 12;
63: if (mem_map[addr]--)
64: return;
65: mem_map[addr]=0;
66: }
67: printk("trying to free free page: memory probably corrupted");
1.1 root 68: }
69:
70: /*
71: * This function frees a continuos block of page tables, as needed
72: * by 'exit()'. As does copy_page_tables(), this handles only 4Mb blocks.
73: */
74: int free_page_tables(unsigned long from,unsigned long size)
75: {
1.1.1.5 root 76: unsigned long page;
77: unsigned long page_dir;
1.1 root 78: unsigned long *pg_table;
79: unsigned long * dir, nr;
80:
81: if (from & 0x3fffff)
82: panic("free_page_tables called with wrong alignment");
83: if (!from)
84: panic("Trying to free up swapper memory space");
85: size = (size + 0x3fffff) >> 22;
86: dir = (unsigned long *) ((from>>20) & 0xffc); /* _pg_dir = 0 */
87: for ( ; size-->0 ; dir++) {
1.1.1.5 root 88: if (!(page_dir = *dir))
1.1 root 89: continue;
90: *dir = 0;
1.1.1.5 root 91: if (!(page_dir & 1)) {
92: printk("free_page_tables: bad page directory.");
93: continue;
94: }
95: pg_table = (unsigned long *) (0xfffff000 & page_dir);
96: for (nr=0 ; nr<1024 ; nr++,pg_table++) {
97: if (!(page = *pg_table))
98: continue;
99: *pg_table = 0;
100: if (1 & page)
101: free_page(0xfffff000 & page);
102: else
103: swap_free(page >> 1);
104: }
105: free_page(0xfffff000 & page_dir);
1.1 root 106: }
107: invalidate();
1.1.1.5 root 108: for (page = 0; page < CHECK_LAST_NR ; page++)
109: last_pages[page] = 0;
1.1 root 110: return 0;
111: }
112:
113: /*
114: * Well, here is one of the most complicated functions in mm. It
115: * copies a range of linerar addresses by copying only the pages.
116: * Let's hope this is bug-free, 'cause this one I don't want to debug :-)
117: *
118: * Note! We don't copy just any chunks of memory - addresses have to
119: * be divisible by 4Mb (one page-directory entry), as this makes the
120: * function easier. It's used only by fork anyway.
121: *
122: * NOTE 2!! When from==0 we are copying kernel space for the first
123: * fork(). Then we DONT want to copy a full page-directory entry, as
124: * that would lead to some serious memory waste - we just copy the
125: * first 160 pages - 640kB. Even that is more than we need, but it
126: * doesn't take any more memory - we don't copy-on-write in the low
127: * 1 Mb-range, so the pages can be shared with the kernel. Thus the
128: * special case for nr=xxxx.
129: */
130: int copy_page_tables(unsigned long from,unsigned long to,long size)
131: {
132: unsigned long * from_page_table;
133: unsigned long * to_page_table;
134: unsigned long this_page;
135: unsigned long * from_dir, * to_dir;
1.1.1.4 root 136: unsigned long new_page;
1.1 root 137: unsigned long nr;
138:
139: if ((from&0x3fffff) || (to&0x3fffff))
140: panic("copy_page_tables called with wrong alignment");
141: from_dir = (unsigned long *) ((from>>20) & 0xffc); /* _pg_dir = 0 */
142: to_dir = (unsigned long *) ((to>>20) & 0xffc);
143: size = ((unsigned) (size+0x3fffff)) >> 22;
144: for( ; size-->0 ; from_dir++,to_dir++) {
1.1.1.5 root 145: if (*to_dir)
146: printk("copy_page_tables: already exist, "
147: "probable memory corruption\n");
148: if (!*from_dir)
149: continue;
150: if (!(1 & *from_dir)) {
151: printk("copy_page_tables: page table swapped out, "
152: "probable memory corruption");
153: *from_dir = 0;
1.1 root 154: continue;
1.1.1.5 root 155: }
1.1 root 156: from_page_table = (unsigned long *) (0xfffff000 & *from_dir);
157: if (!(to_page_table = (unsigned long *) get_free_page()))
158: return -1; /* Out of memory, see freeing */
159: *to_dir = ((unsigned long) to_page_table) | 7;
160: nr = (from==0)?0xA0:1024;
161: for ( ; nr-- > 0 ; from_page_table++,to_page_table++) {
162: this_page = *from_page_table;
1.1.1.4 root 163: if (!this_page)
1.1 root 164: continue;
1.1.1.4 root 165: if (!(1 & this_page)) {
166: if (!(new_page = get_free_page()))
167: return -1;
168: read_swap_page(this_page>>1, (char *) new_page);
169: *to_page_table = this_page;
170: *from_page_table = new_page | (PAGE_DIRTY | 7);
171: continue;
172: }
1.1 root 173: this_page &= ~2;
174: *to_page_table = this_page;
175: if (this_page > LOW_MEM) {
176: *from_page_table = this_page;
177: this_page -= LOW_MEM;
178: this_page >>= 12;
179: mem_map[this_page]++;
180: }
181: }
182: }
183: invalidate();
184: return 0;
185: }
186:
187: /*
188: * This function puts a page in memory at the wanted address.
189: * It returns the physical address of the page gotten, 0 if
190: * out of memory (either when trying to access page-table or
191: * page.)
192: */
1.1.1.4 root 193: static unsigned long put_page(unsigned long page,unsigned long address)
1.1 root 194: {
195: unsigned long tmp, *page_table;
196:
197: /* NOTE !!! This uses the fact that _pg_dir=0 */
198:
1.1.1.5 root 199: if (page < LOW_MEM || page >= HIGH_MEMORY) {
200: printk("put_page: trying to put page %p at %p\n",page,address);
201: return 0;
202: }
203: if (mem_map[(page-LOW_MEM)>>12] != 1) {
1.1 root 204: printk("mem_map disagrees with %p at %p\n",page,address);
1.1.1.5 root 205: return 0;
206: }
1.1 root 207: page_table = (unsigned long *) ((address>>20) & 0xffc);
208: if ((*page_table)&1)
209: page_table = (unsigned long *) (0xfffff000 & *page_table);
210: else {
211: if (!(tmp=get_free_page()))
212: return 0;
1.1.1.4 root 213: *page_table = tmp | 7;
1.1 root 214: page_table = (unsigned long *) tmp;
215: }
1.1.1.6 ! root 216: page_table += (address>>12) & 0x3ff;
! 217: if (*page_table) {
! 218: printk("put_page: page already exists\n");
! 219: *page_table = 0;
! 220: invalidate();
! 221: }
! 222: *page_table = page | 7;
1.1.1.3 root 223: /* no need for invalidate */
1.1 root 224: return page;
225: }
226:
1.1.1.4 root 227: /*
228: * The previous function doesn't work very well if you also want to mark
229: * the page dirty: exec.c wants this, as it has earlier changed the page,
230: * and we want the dirty-status to be correct (for VM). Thus the same
231: * routine, but this time we mark it dirty too.
232: */
233: unsigned long put_dirty_page(unsigned long page, unsigned long address)
234: {
235: unsigned long tmp, *page_table;
236:
237: /* NOTE !!! This uses the fact that _pg_dir=0 */
238:
239: if (page < LOW_MEM || page >= HIGH_MEMORY)
1.1.1.5 root 240: printk("put_dirty_page: trying to put page %p at %p\n",page,address);
1.1.1.4 root 241: if (mem_map[(page-LOW_MEM)>>12] != 1)
242: printk("mem_map disagrees with %p at %p\n",page,address);
243: page_table = (unsigned long *) ((address>>20) & 0xffc);
244: if ((*page_table)&1)
245: page_table = (unsigned long *) (0xfffff000 & *page_table);
246: else {
247: if (!(tmp=get_free_page()))
248: return 0;
249: *page_table = tmp|7;
250: page_table = (unsigned long *) tmp;
251: }
1.1.1.6 ! root 252: page_table += (address>>12) & 0x3ff;
! 253: if (*page_table) {
! 254: printk("put_dirty_page: page already exists\n");
! 255: *page_table = 0;
! 256: invalidate();
! 257: }
! 258: *page_table = page | (PAGE_DIRTY | 7);
1.1.1.4 root 259: /* no need for invalidate */
260: return page;
261: }
262:
1.1 root 263: void un_wp_page(unsigned long * table_entry)
264: {
1.1.1.5 root 265: unsigned long old_page;
266: unsigned long new_page = 0;
267: unsigned long dirty;
268:
269: repeat:
270: old_page = *table_entry;
271: dirty = old_page & PAGE_DIRTY;
272: if (!(old_page & 1)) {
273: if (new_page)
274: free_page(new_page);
275: return;
276: }
277: old_page &= 0xfffff000;
278: if (old_page >= HIGH_MEMORY) {
279: if (new_page)
280: free_page(new_page);
281: printk("bad page address\n\r");
282: do_exit(SIGSEGV);
283: }
1.1 root 284: if (old_page >= LOW_MEM && mem_map[MAP_NR(old_page)]==1) {
285: *table_entry |= 2;
1.1.1.3 root 286: invalidate();
1.1.1.5 root 287: if (new_page)
288: free_page(new_page);
1.1 root 289: return;
290: }
1.1.1.5 root 291: if (!new_page) {
292: if (!(new_page=get_free_page()))
293: oom();
294: goto repeat;
295: }
1.1.1.4 root 296: copy_page(old_page,new_page);
1.1.1.5 root 297: *table_entry = new_page | dirty | 7;
298: free_page(old_page);
1.1.1.3 root 299: invalidate();
1.1 root 300: }
301:
302: /*
303: * This routine handles present pages, when users try to write
304: * to a shared page. It is done by copying the page to a new address
305: * and decrementing the shared-page counter for the old page.
1.1.1.3 root 306: *
307: * If it's in code space we exit with a segment error.
1.1 root 308: */
309: void do_wp_page(unsigned long error_code,unsigned long address)
310: {
1.1.1.6 ! root 311: if (address < TASK_SIZE) {
1.1.1.4 root 312: printk("\n\rBAD! KERNEL MEMORY WP-ERR!\n\r");
1.1.1.6 ! root 313: do_exit(SIGSEGV);
! 314: }
1.1.1.5 root 315: if (address - current->start_code >= TASK_SIZE) {
1.1.1.4 root 316: printk("Bad things happen: page error in do_wp_page\n\r");
317: do_exit(SIGSEGV);
318: }
1.1 root 319: un_wp_page((unsigned long *)
320: (((address>>10) & 0xffc) + (0xfffff000 &
321: *((unsigned long *) ((address>>20) &0xffc)))));
322: }
323:
324: void write_verify(unsigned long address)
325: {
326: unsigned long page;
327:
328: if (!( (page = *((unsigned long *) ((address>>20) & 0xffc)) )&1))
329: return;
330: page &= 0xfffff000;
331: page += ((address>>10) & 0xffc);
332: if ((3 & *(unsigned long *) page) == 1) /* non-writeable, present */
333: un_wp_page((unsigned long *) page);
334: return;
335: }
336:
1.1.1.3 root 337: void get_empty_page(unsigned long address)
338: {
339: unsigned long tmp;
340:
341: if (!(tmp=get_free_page()) || !put_page(tmp,address)) {
342: free_page(tmp); /* 0 is ok - ignored */
343: oom();
344: }
345: }
346:
347: /*
348: * try_to_share() checks the page at address "address" in the task "p",
349: * to see if it exists, and if it is clean. If so, share it with the current
350: * task.
351: *
352: * NOTE! This assumes we have checked that p != current, and that they
1.1.1.4 root 353: * share the same executable or library.
1.1.1.3 root 354: */
355: static int try_to_share(unsigned long address, struct task_struct * p)
356: {
357: unsigned long from;
358: unsigned long to;
359: unsigned long from_page;
360: unsigned long to_page;
361: unsigned long phys_addr;
362:
363: from_page = to_page = ((address>>20) & 0xffc);
364: from_page += ((p->start_code>>20) & 0xffc);
365: to_page += ((current->start_code>>20) & 0xffc);
366: /* is there a page-directory at from? */
367: from = *(unsigned long *) from_page;
368: if (!(from & 1))
369: return 0;
370: from &= 0xfffff000;
371: from_page = from + ((address>>10) & 0xffc);
372: phys_addr = *(unsigned long *) from_page;
373: /* is the page clean and present? */
374: if ((phys_addr & 0x41) != 0x01)
375: return 0;
376: phys_addr &= 0xfffff000;
377: if (phys_addr >= HIGH_MEMORY || phys_addr < LOW_MEM)
378: return 0;
379: to = *(unsigned long *) to_page;
1.1.1.5 root 380: if (!(to & 1)) {
1.1.1.3 root 381: if (to = get_free_page())
382: *(unsigned long *) to_page = to | 7;
383: else
384: oom();
1.1.1.5 root 385: }
1.1.1.3 root 386: to &= 0xfffff000;
387: to_page = to + ((address>>10) & 0xffc);
388: if (1 & *(unsigned long *) to_page)
389: panic("try_to_share: to_page already exists");
390: /* share them: write-protect */
391: *(unsigned long *) from_page &= ~2;
392: *(unsigned long *) to_page = *(unsigned long *) from_page;
393: invalidate();
394: phys_addr -= LOW_MEM;
395: phys_addr >>= 12;
396: mem_map[phys_addr]++;
397: return 1;
398: }
399:
400: /*
401: * share_page() tries to find a process that could share a page with
402: * the current one. Address is the address of the wanted page relative
403: * to the current data space.
404: *
405: * We first check if it is at all feasible by checking executable->i_count.
406: * It should be >1 if there are other tasks sharing this inode.
407: */
1.1.1.5 root 408: static int share_page(struct inode * inode, unsigned long address)
1.1.1.3 root 409: {
410: struct task_struct ** p;
411:
1.1.1.4 root 412: if (inode->i_count < 2 || !inode)
1.1.1.3 root 413: return 0;
414: for (p = &LAST_TASK ; p > &FIRST_TASK ; --p) {
415: if (!*p)
416: continue;
417: if (current == *p)
418: continue;
1.1.1.4 root 419: if (address < LIBRARY_OFFSET) {
420: if (inode != (*p)->executable)
421: continue;
422: } else {
423: if (inode != (*p)->library)
424: continue;
425: }
1.1.1.3 root 426: if (try_to_share(address,*p))
427: return 1;
428: }
429: return 0;
430: }
431:
1.1.1.5 root 432: void do_no_page(unsigned long error_code,
433: unsigned long address, struct task_struct *tsk)
1.1 root 434: {
1.1.1.5 root 435: static unsigned int last_checked = 0;
1.1.1.3 root 436: int nr[4];
1.1 root 437: unsigned long tmp;
1.1.1.3 root 438: unsigned long page;
439: int block,i;
1.1.1.5 root 440: struct inode * inode;
1.1 root 441:
1.1.1.5 root 442: /* Trashing ? Make it interruptible, but don't penalize otherwise */
443: for (i = 0; i < CHECK_LAST_NR; i++)
444: if ((address & 0xfffff000) == last_pages[i]) {
445: current->counter = 0;
446: schedule();
447: }
448: last_checked++;
449: if (last_checked >= CHECK_LAST_NR)
450: last_checked = 0;
451: last_pages[last_checked] = address & 0xfffff000;
1.1.1.6 ! root 452: if (address < TASK_SIZE) {
1.1.1.4 root 453: printk("\n\rBAD!! KERNEL PAGE MISSING\n\r");
1.1.1.6 ! root 454: do_exit(SIGSEGV);
! 455: }
1.1.1.5 root 456: if (address - tsk->start_code >= TASK_SIZE) {
1.1.1.4 root 457: printk("Bad things happen: nonexistent page error in do_no_page\n\r");
458: do_exit(SIGSEGV);
459: }
460: page = *(unsigned long *) ((address >> 20) & 0xffc);
1.1.1.5 root 461: /* check the page directory: make a page dir entry if no such exists */
1.1.1.4 root 462: if (page & 1) {
463: page &= 0xfffff000;
464: page += (address >> 10) & 0xffc;
465: tmp = *(unsigned long *) page;
466: if (tmp && !(1 & tmp)) {
467: swap_in((unsigned long *) page);
468: return;
469: }
1.1.1.5 root 470: } else {
471: if (page)
472: printk("do_no_page: bad page directory\n");
473: if (!(page = get_free_page()))
474: oom();
475: page |= 7;
476: *(unsigned long *) ((address >> 20) & 0xffc) = page;
1.1.1.4 root 477: }
1.1.1.3 root 478: address &= 0xfffff000;
1.1.1.5 root 479: tmp = address - tsk->start_code;
1.1.1.4 root 480: if (tmp >= LIBRARY_OFFSET ) {
1.1.1.5 root 481: inode = tsk->library;
1.1.1.4 root 482: block = 1 + (tmp-LIBRARY_OFFSET) / BLOCK_SIZE;
1.1.1.5 root 483: } else if (tmp < tsk->end_data) {
484: inode = tsk->executable;
1.1.1.4 root 485: block = 1 + tmp / BLOCK_SIZE;
486: } else {
487: inode = NULL;
488: block = 0;
489: }
490: if (!inode) {
1.1.1.3 root 491: get_empty_page(address);
492: return;
493: }
1.1.1.5 root 494: if (tsk == current)
1.1.1.4 root 495: if (share_page(inode,tmp))
1.1.1.3 root 496: return;
497: if (!(page = get_free_page()))
498: oom();
499: /* remember that 1 block is used for header */
500: for (i=0 ; i<4 ; block++,i++)
1.1.1.4 root 501: nr[i] = bmap(inode,block);
502: bread_page(page,inode->i_dev,nr);
1.1.1.5 root 503: i = tmp + 4096 - tsk->end_data;
1.1.1.4 root 504: if (i>4095)
505: i = 0;
1.1.1.3 root 506: tmp = page + 4096;
507: while (i-- > 0) {
508: tmp--;
509: *(char *)tmp = 0;
510: }
511: if (put_page(page,address))
512: return;
513: free_page(page);
514: oom();
1.1 root 515: }
516:
1.1.1.2 root 517: void mem_init(long start_mem, long end_mem)
518: {
519: int i;
520:
1.1.1.5 root 521: swap_device = 0;
522: swap_file = NULL;
1.1.1.2 root 523: HIGH_MEMORY = end_mem;
524: for (i=0 ; i<PAGING_PAGES ; i++)
525: mem_map[i] = USED;
526: i = MAP_NR(start_mem);
527: end_mem -= start_mem;
528: end_mem >>= 12;
529: while (end_mem-->0)
530: mem_map[i++]=0;
531: }
532:
1.1.1.4 root 533: void show_mem(void)
1.1 root 534: {
1.1.1.4 root 535: int i,j,k,free=0,total=0;
536: int shared=0;
537: unsigned long * pg_tbl;
538:
539: printk("Mem-info:\n\r");
540: for(i=0 ; i<PAGING_PAGES ; i++) {
541: if (mem_map[i] == USED)
542: continue;
543: total++;
544: if (!mem_map[i])
545: free++;
546: else
547: shared += mem_map[i]-1;
548: }
549: printk("%d free pages of %d\n\r",free,total);
550: printk("%d pages shared\n\r",shared);
551: k = 0;
552: for(i=4 ; i<1024 ;) {
1.1 root 553: if (1&pg_dir[i]) {
1.1.1.4 root 554: if (pg_dir[i]>HIGH_MEMORY) {
555: printk("page directory[%d]: %08X\n\r",
556: i,pg_dir[i]);
1.1.1.5 root 557: i++;
1.1.1.4 root 558: continue;
559: }
560: if (pg_dir[i]>LOW_MEM)
561: free++,k++;
562: pg_tbl=(unsigned long *) (0xfffff000 & pg_dir[i]);
563: for(j=0 ; j<1024 ; j++)
564: if ((pg_tbl[j]&1) && pg_tbl[j]>LOW_MEM)
565: if (pg_tbl[j]>HIGH_MEMORY)
566: printk("page_dir[%d][%d]: %08X\n\r",
567: i,j, pg_tbl[j]);
568: else
569: k++,free++;
570: }
571: i++;
572: if (!(i&15) && k) {
573: k++,free++; /* one page/process for task_struct */
574: printk("Process %d: %d pages\n\r",(i>>4)-1,k);
575: k = 0;
1.1 root 576: }
577: }
1.1.1.4 root 578: printk("Memory found: %d (%d)\n\r",free-shared,total);
1.1 root 579: }
1.1.1.5 root 580:
581:
582: /* This routine handles page faults. It determines the address,
583: and the problem then passes it off to one of the appropriate
584: routines. */
1.1.1.6 ! root 585: void do_page_fault(unsigned long *esp, unsigned long error_code)
1.1.1.5 root 586: {
587: unsigned long address;
588: /* get the address */
589:
1.1.1.6 ! root 590: __asm__("movl %%cr2,%0":"=r" (address));
1.1.1.5 root 591: if (!(error_code & 1)) {
592: do_no_page(error_code, address, current);
593: return;
594: } else {
595: do_wp_page(error_code, address);
596: return;
597: }
598: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.