|
|
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;
1.1.1.7 root 168: ++current->rss;
1.1.1.4 root 169: read_swap_page(this_page>>1, (char *) new_page);
170: *to_page_table = this_page;
171: *from_page_table = new_page | (PAGE_DIRTY | 7);
172: continue;
173: }
1.1 root 174: this_page &= ~2;
175: *to_page_table = this_page;
176: if (this_page > LOW_MEM) {
177: *from_page_table = this_page;
178: this_page -= LOW_MEM;
179: this_page >>= 12;
180: mem_map[this_page]++;
181: }
182: }
183: }
184: invalidate();
185: return 0;
186: }
187:
188: /*
1.1.1.8 root 189: * a more complete version of free_page_tables which performs with page
190: * granularity.
191: */
1.1.1.10! root 192: int unmap_page_range(unsigned long from, unsigned long size)
1.1.1.8 root 193: {
194: unsigned long page, page_dir;
195: unsigned long *page_table, *dir;
196: unsigned long poff, pcnt, pc;
197:
198: if (from & 0xfff)
199: panic("unmap_page_range called with wrong alignment");
200: if (!from)
201: panic("unmap_page_range trying to free swapper memory space");
202: size = (size + 0xfff) >> 12;
203: dir = (unsigned long *) ((from >> 20) & 0xffc); /* _pg_dir = 0 */
204: poff = (from >> 12) & 0x3ff;
205: if ((pcnt = 1024 - poff) > size)
206: pcnt = size;
207:
208: for ( ; size > 0; ++dir, size -= pcnt,
209: pcnt = (size > 1024 ? 1024 : size)) {
210: if (!(page_dir = *dir)) {
211: poff = 0;
212: continue;
213: }
214: if (!(page_dir & 1)) {
215: printk("unmap_page_range: bad page directory.");
216: continue;
217: }
218: page_table = (unsigned long *)(0xfffff000 & page_dir);
219: if (poff) {
220: page_table += poff;
221: poff = 0;
222: }
223: for (pc = pcnt; pc--; page_table++) {
224: if (page = *page_table) {
225: --current->rss;
226: *page_table = 0;
227: if (1 & page)
228: free_page(0xfffff000 & page);
229: else
230: swap_free(page >> 1);
231: }
232: }
233: if (pcnt == 1024) {
234: free_page(0xfffff000 & page_dir);
235: *dir = 0;
236: }
237: }
238: invalidate();
239: for (page = 0; page < CHECK_LAST_NR ; page++)
240: last_pages[page] = 0;
241: return 0;
242: }
243:
244: /*
245: * maps a range of physical memory into the requested pages. the old
246: * mappings are removed. any references to nonexistent pages results
247: * in null mappings (currently treated as "copy-on-access")
248: *
249: * permiss is encoded as cxwr (copy,exec,write,read) where copy modifies
250: * the behavior of write to be copy-on-write.
251: *
252: * due to current limitations, we actually have the following
253: * on off
254: * read: yes yes
255: * write/copy: yes/copy copy/copy
256: * exec: yes yes
257: */
1.1.1.10! root 258: int remap_page_range(unsigned long from, unsigned long to, unsigned long size,
1.1.1.8 root 259: int permiss)
260: {
261: unsigned long *page_table, *dir;
262: unsigned long poff, pcnt;
263:
264: if ((from & 0xfff) || (to & 0xfff))
265: panic("remap_page_range called with wrong alignment");
266: dir = (unsigned long *) ((from >> 20) & 0xffc); /* _pg_dir = 0 */
267: size = (size + 0xfff) >> 12;
268: poff = (from >> 12) & 0x3ff;
269: if ((pcnt = 1024 - poff) > size)
270: pcnt = size;
271:
272: while (size > 0) {
273: if (!(1 & *dir)) {
274: if (!(page_table = (unsigned long *)get_free_page())) {
275: invalidate();
276: return -1;
277: }
278: *dir++ = ((unsigned long) page_table) | 7;
279: }
280: else
281: page_table = (unsigned long *)(0xfffff000 & *dir++);
282: if (poff) {
283: page_table += poff;
284: poff = 0;
285: }
286:
287: for (size -= pcnt; pcnt-- ;) {
288: int mask;
289:
290: mask = 4;
291: if (permiss & 1)
292: mask |= 1;
293: if (permiss & 2) {
294: if (permiss & 8)
295: mask |= 1;
296: else
297: mask |= 3;
298: }
299: if (permiss & 4)
300: mask |= 1;
301:
302: if (*page_table) {
303: --current->rss;
304: if (1 & *page_table)
305: free_page(0xfffff000 & *page_table);
306: else
307: swap_free(*page_table >> 1);
308: }
309:
310: /*
311: * i'm not sure of the second cond here. should we
312: * report failure?
313: * the first condition should return an invalid access
314: * when the page is referenced. current assumptions
315: * cause it to be treated as demand allocation.
316: */
317: if (mask == 4 || to >= HIGH_MEMORY)
318: *page_table++ = 0; /* not present */
319: else {
320: ++current->rss;
321: *page_table++ = (to | mask);
322: if (to > LOW_MEM) {
323: unsigned long frame;
324: frame = to - LOW_MEM;
325: frame >>= 12;
326: mem_map[frame]++;
327: }
328: }
329: to += PAGE_SIZE;
330: }
331: pcnt = (size > 1024 ? 1024 : size);
332: }
333: invalidate();
334: for (to = 0; to < CHECK_LAST_NR ; to++)
335: last_pages[to] = 0;
336: return 0;
337: }
338:
339: /*
1.1 root 340: * This function puts a page in memory at the wanted address.
341: * It returns the physical address of the page gotten, 0 if
342: * out of memory (either when trying to access page-table or
343: * page.)
344: */
1.1.1.4 root 345: static unsigned long put_page(unsigned long page,unsigned long address)
1.1 root 346: {
347: unsigned long tmp, *page_table;
348:
349: /* NOTE !!! This uses the fact that _pg_dir=0 */
350:
1.1.1.5 root 351: if (page < LOW_MEM || page >= HIGH_MEMORY) {
352: printk("put_page: trying to put page %p at %p\n",page,address);
353: return 0;
354: }
355: if (mem_map[(page-LOW_MEM)>>12] != 1) {
1.1.1.8 root 356: printk("put_page: mem_map disagrees with %p at %p\n",page,address);
1.1.1.5 root 357: return 0;
358: }
1.1 root 359: page_table = (unsigned long *) ((address>>20) & 0xffc);
360: if ((*page_table)&1)
361: page_table = (unsigned long *) (0xfffff000 & *page_table);
362: else {
363: if (!(tmp=get_free_page()))
364: return 0;
1.1.1.4 root 365: *page_table = tmp | 7;
1.1 root 366: page_table = (unsigned long *) tmp;
367: }
1.1.1.6 root 368: page_table += (address>>12) & 0x3ff;
369: if (*page_table) {
370: printk("put_page: page already exists\n");
371: *page_table = 0;
372: invalidate();
373: }
374: *page_table = page | 7;
1.1.1.3 root 375: /* no need for invalidate */
1.1 root 376: return page;
377: }
378:
1.1.1.4 root 379: /*
380: * The previous function doesn't work very well if you also want to mark
381: * the page dirty: exec.c wants this, as it has earlier changed the page,
382: * and we want the dirty-status to be correct (for VM). Thus the same
383: * routine, but this time we mark it dirty too.
384: */
385: unsigned long put_dirty_page(unsigned long page, unsigned long address)
386: {
387: unsigned long tmp, *page_table;
388:
389: /* NOTE !!! This uses the fact that _pg_dir=0 */
390:
391: if (page < LOW_MEM || page >= HIGH_MEMORY)
1.1.1.5 root 392: printk("put_dirty_page: trying to put page %p at %p\n",page,address);
1.1.1.4 root 393: if (mem_map[(page-LOW_MEM)>>12] != 1)
394: printk("mem_map disagrees with %p at %p\n",page,address);
395: page_table = (unsigned long *) ((address>>20) & 0xffc);
396: if ((*page_table)&1)
397: page_table = (unsigned long *) (0xfffff000 & *page_table);
398: else {
399: if (!(tmp=get_free_page()))
400: return 0;
401: *page_table = tmp|7;
402: page_table = (unsigned long *) tmp;
403: }
1.1.1.6 root 404: page_table += (address>>12) & 0x3ff;
405: if (*page_table) {
406: printk("put_dirty_page: page already exists\n");
407: *page_table = 0;
408: invalidate();
409: }
410: *page_table = page | (PAGE_DIRTY | 7);
1.1.1.4 root 411: /* no need for invalidate */
412: return page;
413: }
414:
1.1 root 415: void un_wp_page(unsigned long * table_entry)
416: {
1.1.1.5 root 417: unsigned long old_page;
418: unsigned long new_page = 0;
419: unsigned long dirty;
420:
421: repeat:
422: old_page = *table_entry;
423: dirty = old_page & PAGE_DIRTY;
424: if (!(old_page & 1)) {
425: if (new_page)
426: free_page(new_page);
427: return;
428: }
429: old_page &= 0xfffff000;
430: if (old_page >= HIGH_MEMORY) {
431: if (new_page)
432: free_page(new_page);
433: printk("bad page address\n\r");
434: do_exit(SIGSEGV);
435: }
1.1 root 436: if (old_page >= LOW_MEM && mem_map[MAP_NR(old_page)]==1) {
437: *table_entry |= 2;
1.1.1.3 root 438: invalidate();
1.1.1.5 root 439: if (new_page)
440: free_page(new_page);
1.1 root 441: return;
442: }
1.1.1.5 root 443: if (!new_page) {
444: if (!(new_page=get_free_page()))
445: oom();
446: goto repeat;
447: }
1.1.1.4 root 448: copy_page(old_page,new_page);
1.1.1.5 root 449: *table_entry = new_page | dirty | 7;
450: free_page(old_page);
1.1.1.3 root 451: invalidate();
1.1 root 452: }
453:
454: /*
455: * This routine handles present pages, when users try to write
456: * to a shared page. It is done by copying the page to a new address
457: * and decrementing the shared-page counter for the old page.
1.1.1.3 root 458: *
459: * If it's in code space we exit with a segment error.
1.1 root 460: */
461: void do_wp_page(unsigned long error_code,unsigned long address)
462: {
1.1.1.6 root 463: if (address < TASK_SIZE) {
1.1.1.4 root 464: printk("\n\rBAD! KERNEL MEMORY WP-ERR!\n\r");
1.1.1.6 root 465: do_exit(SIGSEGV);
466: }
1.1.1.5 root 467: if (address - current->start_code >= TASK_SIZE) {
1.1.1.4 root 468: printk("Bad things happen: page error in do_wp_page\n\r");
469: do_exit(SIGSEGV);
470: }
1.1.1.7 root 471: ++current->min_flt;
1.1 root 472: un_wp_page((unsigned long *)
473: (((address>>10) & 0xffc) + (0xfffff000 &
474: *((unsigned long *) ((address>>20) &0xffc)))));
475: }
476:
477: void write_verify(unsigned long address)
478: {
479: unsigned long page;
480:
481: if (!( (page = *((unsigned long *) ((address>>20) & 0xffc)) )&1))
482: return;
483: page &= 0xfffff000;
484: page += ((address>>10) & 0xffc);
485: if ((3 & *(unsigned long *) page) == 1) /* non-writeable, present */
486: un_wp_page((unsigned long *) page);
487: return;
488: }
489:
1.1.1.10! root 490: static void get_empty_page(unsigned long address)
1.1.1.3 root 491: {
492: unsigned long tmp;
493:
494: if (!(tmp=get_free_page()) || !put_page(tmp,address)) {
495: free_page(tmp); /* 0 is ok - ignored */
496: oom();
497: }
498: }
499:
500: /*
501: * try_to_share() checks the page at address "address" in the task "p",
502: * to see if it exists, and if it is clean. If so, share it with the current
503: * task.
504: *
505: * NOTE! This assumes we have checked that p != current, and that they
1.1.1.4 root 506: * share the same executable or library.
1.1.1.3 root 507: */
508: static int try_to_share(unsigned long address, struct task_struct * p)
509: {
510: unsigned long from;
511: unsigned long to;
512: unsigned long from_page;
513: unsigned long to_page;
514: unsigned long phys_addr;
515:
516: from_page = to_page = ((address>>20) & 0xffc);
517: from_page += ((p->start_code>>20) & 0xffc);
518: to_page += ((current->start_code>>20) & 0xffc);
519: /* is there a page-directory at from? */
520: from = *(unsigned long *) from_page;
521: if (!(from & 1))
522: return 0;
523: from &= 0xfffff000;
524: from_page = from + ((address>>10) & 0xffc);
525: phys_addr = *(unsigned long *) from_page;
526: /* is the page clean and present? */
527: if ((phys_addr & 0x41) != 0x01)
528: return 0;
529: phys_addr &= 0xfffff000;
530: if (phys_addr >= HIGH_MEMORY || phys_addr < LOW_MEM)
531: return 0;
532: to = *(unsigned long *) to_page;
1.1.1.5 root 533: if (!(to & 1)) {
1.1.1.3 root 534: if (to = get_free_page())
535: *(unsigned long *) to_page = to | 7;
536: else
537: oom();
1.1.1.5 root 538: }
1.1.1.3 root 539: to &= 0xfffff000;
540: to_page = to + ((address>>10) & 0xffc);
541: if (1 & *(unsigned long *) to_page)
542: panic("try_to_share: to_page already exists");
543: /* share them: write-protect */
544: *(unsigned long *) from_page &= ~2;
545: *(unsigned long *) to_page = *(unsigned long *) from_page;
546: invalidate();
547: phys_addr -= LOW_MEM;
548: phys_addr >>= 12;
549: mem_map[phys_addr]++;
550: return 1;
551: }
552:
553: /*
554: * share_page() tries to find a process that could share a page with
555: * the current one. Address is the address of the wanted page relative
556: * to the current data space.
557: *
558: * We first check if it is at all feasible by checking executable->i_count.
559: * It should be >1 if there are other tasks sharing this inode.
560: */
1.1.1.5 root 561: static int share_page(struct inode * inode, unsigned long address)
1.1.1.3 root 562: {
563: struct task_struct ** p;
1.1.1.8 root 564: int i;
1.1.1.3 root 565:
1.1.1.9 root 566: if (!inode || inode->i_count < 2)
1.1.1.3 root 567: return 0;
568: for (p = &LAST_TASK ; p > &FIRST_TASK ; --p) {
569: if (!*p)
570: continue;
571: if (current == *p)
572: continue;
1.1.1.4 root 573: if (address < LIBRARY_OFFSET) {
574: if (inode != (*p)->executable)
575: continue;
576: } else {
1.1.1.8 root 577: for (i=0; i < (*p)->numlibraries; i++)
578: if (inode == (*p)->libraries[i].library)
579: break;
580: if (i >= (*p)->numlibraries)
1.1.1.4 root 581: continue;
582: }
1.1.1.3 root 583: if (try_to_share(address,*p))
584: return 1;
585: }
586: return 0;
587: }
588:
1.1.1.8 root 589: /*
590: * fill in an empty page or directory if none exists
591: */
592: static unsigned long get_empty(unsigned long * p)
593: {
594: unsigned long page = 0;
595:
596: repeat:
597: if (1 & *p) {
598: free_page(page);
599: return *p;
600: }
601: if (*p) {
602: printk("get_empty: bad page entry \n");
603: *p = 0;
604: }
605: if (page) {
606: *p = page | 7;
607: return *p;
608: }
609: if (!(page = get_free_page()))
610: oom();
611: goto repeat;
612: }
613:
1.1.1.7 root 614: void do_no_page(unsigned long error_code, unsigned long address,
1.1.1.8 root 615: struct task_struct *tsk, unsigned long user_esp)
1.1 root 616: {
1.1.1.5 root 617: static unsigned int last_checked = 0;
1.1.1.3 root 618: int nr[4];
1.1 root 619: unsigned long tmp;
1.1.1.3 root 620: unsigned long page;
1.1.1.8 root 621: unsigned int block,i;
1.1.1.5 root 622: struct inode * inode;
1.1 root 623:
1.1.1.7 root 624: /* Thrashing ? Make it interruptible, but don't penalize otherwise */
1.1.1.5 root 625: for (i = 0; i < CHECK_LAST_NR; i++)
626: if ((address & 0xfffff000) == last_pages[i]) {
627: current->counter = 0;
628: schedule();
629: }
630: last_checked++;
631: if (last_checked >= CHECK_LAST_NR)
632: last_checked = 0;
633: last_pages[last_checked] = address & 0xfffff000;
1.1.1.6 root 634: if (address < TASK_SIZE) {
1.1.1.4 root 635: printk("\n\rBAD!! KERNEL PAGE MISSING\n\r");
1.1.1.6 root 636: do_exit(SIGSEGV);
637: }
1.1.1.5 root 638: if (address - tsk->start_code >= TASK_SIZE) {
1.1.1.4 root 639: printk("Bad things happen: nonexistent page error in do_no_page\n\r");
640: do_exit(SIGSEGV);
641: }
1.1.1.8 root 642: page = get_empty((unsigned long *) ((address >> 20) & 0xffc));
643: page &= 0xfffff000;
644: page += (address >> 10) & 0xffc;
645: tmp = *(unsigned long *) page;
646: if (tmp & 1) {
647: printk("bogus do_no_page\n");
648: return;
649: }
1.1.1.7 root 650: ++tsk->rss;
1.1.1.8 root 651: if (tmp) {
652: ++tsk->maj_flt;
653: swap_in((unsigned long *) page);
654: return;
1.1.1.4 root 655: }
1.1.1.3 root 656: address &= 0xfffff000;
1.1.1.5 root 657: tmp = address - tsk->start_code;
1.1.1.8 root 658: inode = NULL;
659: block = 0;
660: if (tmp < tsk->end_data) {
1.1.1.5 root 661: inode = tsk->executable;
1.1.1.4 root 662: block = 1 + tmp / BLOCK_SIZE;
663: } else {
1.1.1.8 root 664: i = tsk->numlibraries;
665: while (i-- > 0) {
666: if (tmp < tsk->libraries[i].start)
667: continue;
668: block = tmp - tsk->libraries[i].start;
669: if (block >= tsk->libraries[i].length)
670: continue;
671: inode = tsk->libraries[i].library;
672: block = 1 + block / BLOCK_SIZE;
673: break;
674: }
1.1.1.4 root 675: }
676: if (!inode) {
1.1.1.7 root 677: ++tsk->min_flt;
1.1.1.3 root 678: get_empty_page(address);
1.1.1.8 root 679: if (tsk != current)
680: return;
681: if (tmp >= LIBRARY_OFFSET || tmp < tsk->brk)
682: return;
683: if (tmp+8192 >= (user_esp & 0xfffff000))
684: return;
685: send_sig(SIGSEGV,tsk,1);
1.1.1.3 root 686: return;
687: }
1.1.1.5 root 688: if (tsk == current)
1.1.1.7 root 689: if (share_page(inode,tmp)) {
690: ++tsk->min_flt;
691: return;
692: }
693: ++tsk->maj_flt;
1.1.1.3 root 694: if (!(page = get_free_page()))
695: oom();
696: for (i=0 ; i<4 ; block++,i++)
1.1.1.4 root 697: nr[i] = bmap(inode,block);
698: bread_page(page,inode->i_dev,nr);
1.1.1.5 root 699: i = tmp + 4096 - tsk->end_data;
1.1.1.4 root 700: if (i>4095)
701: i = 0;
1.1.1.3 root 702: tmp = page + 4096;
1.1.1.8 root 703: while (i--) {
1.1.1.3 root 704: tmp--;
705: *(char *)tmp = 0;
706: }
707: if (put_page(page,address))
708: return;
709: free_page(page);
710: oom();
1.1 root 711: }
712:
1.1.1.2 root 713: void mem_init(long start_mem, long end_mem)
714: {
715: int i;
716:
1.1.1.9 root 717: end_mem &= 0xfffff000;
718: start_mem += 0xfff;
719: start_mem &= 0xfffff000;
1.1.1.5 root 720: swap_device = 0;
721: swap_file = NULL;
1.1.1.2 root 722: HIGH_MEMORY = end_mem;
723: for (i=0 ; i<PAGING_PAGES ; i++)
724: mem_map[i] = USED;
725: i = MAP_NR(start_mem);
726: end_mem -= start_mem;
727: end_mem >>= 12;
728: while (end_mem-->0)
729: mem_map[i++]=0;
730: }
731:
1.1.1.4 root 732: void show_mem(void)
1.1 root 733: {
1.1.1.4 root 734: int i,j,k,free=0,total=0;
1.1.1.7 root 735: int shared = 0;
1.1.1.4 root 736: unsigned long * pg_tbl;
737:
738: printk("Mem-info:\n\r");
739: for(i=0 ; i<PAGING_PAGES ; i++) {
740: if (mem_map[i] == USED)
741: continue;
742: total++;
743: if (!mem_map[i])
744: free++;
745: else
746: shared += mem_map[i]-1;
747: }
748: printk("%d free pages of %d\n\r",free,total);
749: printk("%d pages shared\n\r",shared);
750: k = 0;
751: for(i=4 ; i<1024 ;) {
1.1 root 752: if (1&pg_dir[i]) {
1.1.1.4 root 753: if (pg_dir[i]>HIGH_MEMORY) {
754: printk("page directory[%d]: %08X\n\r",
755: i,pg_dir[i]);
1.1.1.5 root 756: i++;
1.1.1.4 root 757: continue;
758: }
759: if (pg_dir[i]>LOW_MEM)
760: free++,k++;
761: pg_tbl=(unsigned long *) (0xfffff000 & pg_dir[i]);
762: for(j=0 ; j<1024 ; j++)
763: if ((pg_tbl[j]&1) && pg_tbl[j]>LOW_MEM)
764: if (pg_tbl[j]>HIGH_MEMORY)
765: printk("page_dir[%d][%d]: %08X\n\r",
766: i,j, pg_tbl[j]);
767: else
768: k++,free++;
769: }
770: i++;
771: if (!(i&15) && k) {
772: k++,free++; /* one page/process for task_struct */
773: printk("Process %d: %d pages\n\r",(i>>4)-1,k);
774: k = 0;
1.1 root 775: }
776: }
1.1.1.4 root 777: printk("Memory found: %d (%d)\n\r",free-shared,total);
1.1 root 778: }
1.1.1.5 root 779:
780:
781: /* This routine handles page faults. It determines the address,
782: and the problem then passes it off to one of the appropriate
783: routines. */
1.1.1.6 root 784: void do_page_fault(unsigned long *esp, unsigned long error_code)
1.1.1.5 root 785: {
786: unsigned long address;
1.1.1.8 root 787: unsigned long user_esp;
1.1.1.5 root 788:
1.1.1.8 root 789: if ((0xffff & esp[1]) == 0xf)
790: user_esp = esp[3];
791: else
792: user_esp = 0;
793: /* get the address */
1.1.1.6 root 794: __asm__("movl %%cr2,%0":"=r" (address));
1.1.1.5 root 795: if (!(error_code & 1)) {
1.1.1.8 root 796: do_no_page(error_code, address, current, user_esp);
1.1.1.5 root 797: return;
798: } else {
799: do_wp_page(error_code, address);
800: return;
801: }
802: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.