root/arch/i386/mm/init.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. __bad_pagetable
  2. __bad_page
  3. show_mem
  4. paging_init
  5. mem_init
  6. si_meminfo

   1 /*
   2  *  linux/arch/i386/mm/init.c
   3  *
   4  *  Copyright (C) 1995  Linus Torvalds
   5  */
   6 
   7 #include <linux/config.h>
   8 #include <linux/signal.h>
   9 #include <linux/sched.h>
  10 #include <linux/head.h>
  11 #include <linux/kernel.h>
  12 #include <linux/errno.h>
  13 #include <linux/string.h>
  14 #include <linux/types.h>
  15 #include <linux/ptrace.h>
  16 #include <linux/mman.h>
  17 #include <linux/mm.h>
  18 #include <linux/smp.h>
  19 
  20 #include <asm/system.h>
  21 #include <asm/segment.h>
  22 #include <asm/pgtable.h>
  23 
  24 extern void scsi_mem_init(unsigned long);
  25 extern void sound_mem_init(void);
  26 extern void die_if_kernel(char *,struct pt_regs *,long);
  27 extern void show_net_buffers(void);
  28 
  29 /*
  30  * BAD_PAGE is the page that is used for page faults when linux
  31  * is out-of-memory. Older versions of linux just did a
  32  * do_exit(), but using this instead means there is less risk
  33  * for a process dying in kernel mode, possibly leaving a inode
  34  * unused etc..
  35  *
  36  * BAD_PAGETABLE is the accompanying page-table: it is initialized
  37  * to point to BAD_PAGE entries.
  38  *
  39  * ZERO_PAGE is a special page that is used for zero-initialized
  40  * data and COW.
  41  */
  42 pte_t * __bad_pagetable(void)
     /* [previous][next][first][last][top][bottom][index][help] */
  43 {
  44         extern char empty_bad_page_table[PAGE_SIZE];
  45 
  46         __asm__ __volatile__("cld ; rep ; stosl":
  47                 :"a" (pte_val(BAD_PAGE)),
  48                  "D" ((long) empty_bad_page_table),
  49                  "c" (PAGE_SIZE/4)
  50                 :"di","cx");
  51         return (pte_t *) empty_bad_page_table;
  52 }
  53 
  54 pte_t __bad_page(void)
     /* [previous][next][first][last][top][bottom][index][help] */
  55 {
  56         extern char empty_bad_page[PAGE_SIZE];
  57 
  58         __asm__ __volatile__("cld ; rep ; stosl":
  59                 :"a" (0),
  60                  "D" ((long) empty_bad_page),
  61                  "c" (PAGE_SIZE/4)
  62                 :"di","cx");
  63         return pte_mkdirty(mk_pte((unsigned long) empty_bad_page, PAGE_SHARED));
  64 }
  65 
  66 void show_mem(void)
     /* [previous][next][first][last][top][bottom][index][help] */
  67 {
  68         int i,free = 0,total = 0,reserved = 0;
  69         int shared = 0;
  70 
  71         printk("Mem-info:\n");
  72         show_free_areas();
  73         printk("Free swap:       %6dkB\n",nr_swap_pages<<(PAGE_SHIFT-10));
  74         i = high_memory >> PAGE_SHIFT;
  75         while (i-- > 0) {
  76                 total++;
  77                 if (mem_map[i].reserved)
  78                         reserved++;
  79                 else if (!mem_map[i].count)
  80                         free++;
  81                 else
  82                         shared += mem_map[i].count-1;
  83         }
  84         printk("%d pages of RAM\n",total);
  85         printk("%d free pages\n",free);
  86         printk("%d reserved pages\n",reserved);
  87         printk("%d pages shared\n",shared);
  88         show_buffers();
  89 #ifdef CONFIG_NET
  90         show_net_buffers();
  91 #endif
  92 }
  93 
  94 extern unsigned long free_area_init(unsigned long, unsigned long);
  95 
  96 /*
  97  * paging_init() sets up the page tables - note that the first 4MB are
  98  * already mapped by head.S.
  99  *
 100  * This routines also unmaps the page at virtual kernel address 0, so
 101  * that we can trap those pesky NULL-reference errors in the kernel.
 102  */
 103 unsigned long paging_init(unsigned long start_mem, unsigned long end_mem)
     /* [previous][next][first][last][top][bottom][index][help] */
 104 {
 105         pgd_t * pg_dir;
 106         pte_t * pg_table;
 107         unsigned long tmp;
 108         unsigned long address;
 109 
 110 /*
 111  * Physical page 0 is special; it's not touched by Linux since BIOS
 112  * and SMM (for laptops with [34]86/SL chips) may need it.  It is read
 113  * and write protected to detect null pointer references in the
 114  * kernel.
 115  * It may also hold the MP configuration table when we are booting SMP.
 116  */
 117 #if 0
 118         memset((void *) 0, 0, PAGE_SIZE);
 119 #endif
 120 #ifdef CONFIG_SMP
 121         smp_scan_config(0x0,0x400);     /* Scan the bottom 1K for a signature */
 122         /*
 123          *      FIXME: Linux assumes you have 640K of base ram.. this continues
 124          *      the error...
 125          */
 126         smp_scan_config(639*0x400,0x400);       /* Scan the top 1K of base RAM */
 127         smp_scan_config(0xF0000,0x10000);       /* Scan the 64K of bios */
 128         /*
 129          *      If it is an SMP machine we should know now, unless the configuration
 130          *      is in an EISA/MCA bus machine with an extended bios data area. I don't
 131          *      have such a machine so someone else can fill in the check of the EBDA
 132          *      here.
 133          */
 134 /*      smp_alloc_memory(8192); */
 135 #endif
 136 #ifdef CONFIG_TEST_VERIFY_AREA
 137         wp_works_ok = 0;
 138 #endif
 139         start_mem = PAGE_ALIGN(start_mem);
 140         address = 0;
 141         pg_dir = swapper_pg_dir;
 142         while (address < end_mem) {
 143 #ifdef CONFIG_PENTIUM_MM
 144 #ifndef CONFIG_SMP
 145                 if (address <= end_mem + 4*1024*1024 &&
 146                     (x86_capability & 8)) {
 147 #ifdef GAS_KNOWS_CR4
 148                         __asm__("movl %%cr4,%%eax\n\t"
 149                                 "orl $16,%%eax\n\t"
 150                                 "movl %%eax,%%cr4"
 151                                 : : :"ax");
 152 #else
 153                         __asm__(".byte 0x0f,0x20,0xe0\n\t"
 154                                 "orl $16,%%eax\n\t"
 155                                 ".byte 0x0f,0x22,0xe0"
 156                                 : : :"ax");
 157 #endif
 158                         wp_works_ok = 1;
 159                         pgd_val(pg_dir[0]) = _PAGE_TABLE | _PAGE_4M | address;
 160                         pgd_val(pg_dir[768]) = _PAGE_TABLE | _PAGE_4M | address;
 161                         pg_dir++;
 162                         address += 4*1024*1024;
 163                         continue;
 164                 }
 165 #endif
 166 #endif
 167                 /* map the memory at virtual addr 0xC0000000 */
 168                 pg_table = (pte_t *) (PAGE_MASK & pgd_val(pg_dir[768]));
 169                 if (!pg_table) {
 170                         pg_table = (pte_t *) start_mem;
 171                         start_mem += PAGE_SIZE;
 172                 }
 173 
 174                 /* also map it temporarily at 0x0000000 for init */
 175                 pgd_val(pg_dir[0])   = _PAGE_TABLE | (unsigned long) pg_table;
 176                 pgd_val(pg_dir[768]) = _PAGE_TABLE | (unsigned long) pg_table;
 177                 pg_dir++;
 178                 for (tmp = 0 ; tmp < PTRS_PER_PTE ; tmp++,pg_table++) {
 179                         if (address < end_mem)
 180                                 set_pte(pg_table, mk_pte(address, PAGE_SHARED));
 181                         else
 182                                 pte_clear(pg_table);
 183                         address += PAGE_SIZE;
 184                 }
 185         }
 186         invalidate();
 187         return free_area_init(start_mem, end_mem);
 188 }
 189 
 190 void mem_init(unsigned long start_mem, unsigned long end_mem)
     /* [previous][next][first][last][top][bottom][index][help] */
 191 {
 192         unsigned long start_low_mem = PAGE_SIZE;
 193         int codepages = 0;
 194         int reservedpages = 0;
 195         int datapages = 0;
 196         unsigned long tmp;
 197         extern int _etext;
 198 
 199         end_mem &= PAGE_MASK;
 200         high_memory = end_mem;
 201 
 202         /* clear the zero-page */
 203         memset(empty_zero_page, 0, PAGE_SIZE);
 204 
 205         /* mark usable pages in the mem_map[] */
 206         start_low_mem = PAGE_ALIGN(start_low_mem);
 207 
 208 #ifdef CONFIG_SMP
 209         /*
 210          * But first pinch a few for the stack/trampoline stuff
 211          */
 212         start_low_mem += PAGE_SIZE;                             /* 32bit startup code */
 213         start_low_mem = smp_alloc_memory(start_low_mem);        /* AP processor stacks */
 214 #endif
 215         start_mem = PAGE_ALIGN(start_mem);
 216 
 217         /*
 218          * IBM messed up *AGAIN* in their thinkpad: 0xA0000 -> 0x9F000.
 219          * They seem to have done something stupid with the floppy
 220          * controller as well..
 221          */
 222         while (start_low_mem < 0x9f000) {
 223                 mem_map[MAP_NR(start_low_mem)].reserved = 0;
 224                 start_low_mem += PAGE_SIZE;
 225         }
 226 
 227         while (start_mem < high_memory) {
 228                 mem_map[MAP_NR(start_mem)].reserved = 0;
 229                 start_mem += PAGE_SIZE;
 230         }
 231 #ifdef CONFIG_SCSI
 232         scsi_mem_init(high_memory);
 233 #endif
 234 #ifdef CONFIG_SOUND
 235         sound_mem_init();
 236 #endif
 237         for (tmp = 0 ; tmp < high_memory ; tmp += PAGE_SIZE) {
 238                 if (mem_map[MAP_NR(tmp)].reserved) {
 239                         if (tmp >= 0xA0000 && tmp < 0x100000)
 240                                 reservedpages++;
 241                         else if (tmp < (unsigned long) &_etext)
 242                                 codepages++;
 243                         else
 244                                 datapages++;
 245                         continue;
 246                 }
 247                 mem_map[MAP_NR(tmp)].count = 1;
 248                 free_page(tmp);
 249         }
 250         tmp = nr_free_pages << PAGE_SHIFT;
 251         printk("Memory: %luk/%luk available (%dk kernel code, %dk reserved, %dk data)\n",
 252                 tmp >> 10,
 253                 high_memory >> 10,
 254                 codepages << (PAGE_SHIFT-10),
 255                 reservedpages << (PAGE_SHIFT-10),
 256                 datapages << (PAGE_SHIFT-10));
 257 /* test if the WP bit is honoured in supervisor mode */
 258         if (wp_works_ok < 0) {
 259                 pg0[0] = pte_val(mk_pte(0, PAGE_READONLY));
 260                 invalidate();
 261                 __asm__ __volatile__("movb 0,%%al ; movb %%al,0": : :"ax", "memory");
 262                 pg0[0] = 0;
 263                 invalidate();
 264                 if (wp_works_ok < 0)
 265                         wp_works_ok = 0;
 266         }
 267         return;
 268 }
 269 
 270 void si_meminfo(struct sysinfo *val)
     /* [previous][next][first][last][top][bottom][index][help] */
 271 {
 272         int i;
 273 
 274         i = high_memory >> PAGE_SHIFT;
 275         val->totalram = 0;
 276         val->sharedram = 0;
 277         val->freeram = nr_free_pages << PAGE_SHIFT;
 278         val->bufferram = buffermem;
 279         while (i-- > 0)  {
 280                 if (mem_map[i].reserved)
 281                         continue;
 282                 val->totalram++;
 283                 if (!mem_map[i].count)
 284                         continue;
 285                 val->sharedram += mem_map[i].count-1;
 286         }
 287         val->totalram <<= PAGE_SHIFT;
 288         val->sharedram <<= PAGE_SHIFT;
 289         return;
 290 }

/* [previous][next][first][last][top][bottom][index][help] */