ia64/xen-unstable

view extras/mini-os/include/mm.h @ 6435:b4b3f6be5226

merge?
author cl349@firebug.cl.cam.ac.uk
date Thu Aug 25 17:27:49 2005 +0000 (2005-08-25)
parents 0610add7c3fe 98a6eb458c78
children 8799d14bef77 9312a3e8a6f8
line source
1 /* -*- Mode:C; c-basic-offset:4; tab-width:4 -*-
2 *
3 * (C) 2003 - Rolf Neugebauer - Intel Research Cambridge
4 * Copyright (c) 2005, Keir A Fraser
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 */
25 #ifndef _MM_H_
26 #define _MM_H_
28 #ifdef __x86_64__
30 #define L1_PAGETABLE_SHIFT 12
31 #define L2_PAGETABLE_SHIFT 21
32 #define L3_PAGETABLE_SHIFT 30
33 #define L4_PAGETABLE_SHIFT 39
35 #define L1_PAGETABLE_ENTRIES 512
36 #define L2_PAGETABLE_ENTRIES 512
37 #define L3_PAGETABLE_ENTRIES 512
38 #define L4_PAGETABLE_ENTRIES 512
40 /* These are page-table limitations. Current CPUs support only 40-bit phys. */
41 #define PADDR_BITS 52
42 #define VADDR_BITS 48
43 #define PADDR_MASK ((1UL << PADDR_BITS)-1)
44 #define VADDR_MASK ((1UL << VADDR_BITS)-1)
46 #define pte_to_mfn(_pte) (((_pte) & (PADDR_MASK&PAGE_MASK)) >> L1_PAGETABLE_SHIFT)
48 #endif
52 #ifdef __i386__
54 #define L1_PAGETABLE_SHIFT 12
55 #define L2_PAGETABLE_SHIFT 22
57 #define L1_PAGETABLE_ENTRIES 1024
58 #define L2_PAGETABLE_ENTRIES 1024
59 #endif
61 /* Given a virtual address, get an entry offset into a page table. */
62 #define l1_table_offset(_a) \
63 (((_a) >> L1_PAGETABLE_SHIFT) & (L1_PAGETABLE_ENTRIES - 1))
64 #define l2_table_offset(_a) \
65 (((_a) >> L2_PAGETABLE_SHIFT) & (L2_PAGETABLE_ENTRIES - 1))
66 #ifdef __x86_64__
67 #define l3_table_offset(_a) \
68 (((_a) >> L3_PAGETABLE_SHIFT) & (L3_PAGETABLE_ENTRIES - 1))
69 #define l4_table_offset(_a) \
70 (((_a) >> L4_PAGETABLE_SHIFT) & (L4_PAGETABLE_ENTRIES - 1))
71 #endif
73 #define _PAGE_PRESENT 0x001UL
74 #define _PAGE_RW 0x002UL
75 #define _PAGE_USER 0x004UL
76 #define _PAGE_PWT 0x008UL
77 #define _PAGE_PCD 0x010UL
78 #define _PAGE_ACCESSED 0x020UL
79 #define _PAGE_DIRTY 0x040UL
80 #define _PAGE_PAT 0x080UL
81 #define _PAGE_PSE 0x080UL
82 #define _PAGE_GLOBAL 0x100UL
84 #define L1_PROT (_PAGE_PRESENT | _PAGE_RW | _PAGE_ACCESSED)
85 #define L2_PROT (_PAGE_PRESENT | _PAGE_RW | _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_USER)
87 #define PAGE_SIZE (1UL << L1_PAGETABLE_SHIFT)
88 #define PAGE_SHIFT L1_PAGETABLE_SHIFT
89 #define PAGE_MASK (~(PAGE_SIZE-1))
91 #define PFN_UP(x) (((x) + PAGE_SIZE-1) >> L1_PAGETABLE_SHIFT)
92 #define PFN_DOWN(x) ((x) >> L1_PAGETABLE_SHIFT)
93 #define PFN_PHYS(x) ((x) << L1_PAGETABLE_SHIFT)
95 /* to align the pointer to the (next) page boundary */
96 #define PAGE_ALIGN(addr) (((addr)+PAGE_SIZE-1)&PAGE_MASK)
98 extern unsigned long *phys_to_machine_mapping;
99 #define pfn_to_mfn(_pfn) (phys_to_machine_mapping[(_pfn)])
100 #define mfn_to_pfn(_mfn) (machine_to_phys_mapping[(_mfn)])
101 static __inline__ unsigned long phys_to_machine(unsigned long phys)
102 {
103 unsigned long machine = pfn_to_mfn(phys >> L1_PAGETABLE_SHIFT);
104 machine = (machine << L1_PAGETABLE_SHIFT) | (phys & ~PAGE_MASK);
105 return machine;
106 }
107 static __inline__ unsigned long machine_to_phys(unsigned long machine)
108 {
109 unsigned long phys = mfn_to_pfn(machine >> L1_PAGETABLE_SHIFT);
110 phys = (phys << L1_PAGETABLE_SHIFT) | (machine & ~PAGE_MASK);
111 return phys;
112 }
114 #ifdef __x86_64__
115 #define VIRT_START 0xFFFFFFFF00000000UL
116 #else
117 #define VIRT_START 0xC0000000UL
118 #endif
120 #define to_phys(x) ((unsigned long)(x)-VIRT_START)
121 #define to_virt(x) ((void *)((unsigned long)(x)+VIRT_START))
122 #define __va to_virt
123 #define __pa to_phys
125 #define virt_to_pfn(_virt) (PFN_DOWN(to_phys(_virt)))
127 void init_mm(void);
128 unsigned long alloc_pages(int order);
129 int is_mfn_mapped(unsigned long mfn);
131 #endif /* _MM_H_ */