xref: /titanic_41/usr/src/uts/i86pc/vm/hat_i86.h (revision 2a3221a4fea0ec9c322f85a262eab79e762d32f2)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #ifndef	_VM_HAT_I86_H
27 #define	_VM_HAT_I86_H
28 
29 #pragma ident	"%Z%%M%	%I%	%E% SMI"
30 
31 #ifdef	__cplusplus
32 extern "C" {
33 #endif
34 
35 /*
36  * VM - Hardware Address Translation management.
37  *
38  * This file describes the contents of the x86_64 HAT data structures.
39  */
40 #include <sys/types.h>
41 #include <sys/t_lock.h>
42 #include <sys/cpuvar.h>
43 #include <sys/x_call.h>
44 #include <vm/seg.h>
45 #include <vm/page.h>
46 #include <sys/vmparam.h>
47 #include <sys/vm_machparam.h>
48 #include <sys/promif.h>
49 #include <vm/hat_pte.h>
50 #include <vm/htable.h>
51 #include <vm/hment.h>
52 
53 /*
54  * The essential data types involved:
55  *
56  * htable_t	- There is one of these for each page table and it is used
57  *		by the HAT to manage the page table.
58  *
59  * hment_t	- Links together multiple PTEs to a single page.
60  */
61 
62 /*
63  * VLP processes have a 32 bit address range, so their top level is 2 and
64  * with only 4 PTEs in that table.
65  */
66 #define	VLP_LEVEL	(2)
67 #define	VLP_NUM_PTES	(4)
68 #define	VLP_SIZE	(VLP_NUM_PTES * sizeof (x86pte_t))
69 #define	TOP_LEVEL(h)	(((h)->hat_flags & HAT_VLP) ? VLP_LEVEL : mmu.max_level)
70 #define	VLP_COPY(fromptep, toptep) { \
71 	toptep[0] = fromptep[0]; \
72 	toptep[1] = fromptep[1]; \
73 	toptep[2] = fromptep[2]; \
74 	toptep[3] = fromptep[3]; \
75 }
76 
77 /*
78  * The hat struct exists for each address space.
79  */
80 struct hat {
81 	kmutex_t	hat_mutex;
82 	kmutex_t	hat_switch_mutex;
83 	struct as	*hat_as;
84 	uint_t		hat_stats;
85 	pgcnt_t		hat_pages_mapped[MAX_PAGE_LEVEL + 1];
86 	cpuset_t	hat_cpus;
87 	uint16_t	hat_flags;
88 	htable_t	*hat_htable;	/* top level htable */
89 	struct hat	*hat_next;
90 	struct hat	*hat_prev;
91 	uint_t		hat_num_hash;	/* number of htable hash buckets */
92 	htable_t	**hat_ht_hash;	/* htable hash buckets */
93 	htable_t	*hat_ht_cached;	/* cached free htables */
94 	x86pte_t	hat_vlp_ptes[VLP_NUM_PTES];
95 };
96 typedef struct hat hat_t;
97 
98 #define	PGCNT_INC(hat, level) \
99 	atomic_add_long(&(hat)->hat_pages_mapped[level], 1);
100 #define	PGCNT_DEC(hat, level) \
101 	atomic_add_long(&(hat)->hat_pages_mapped[level], -1);
102 
103 /*
104  * Flags for the hat_flags field
105  *
106  * HAT_FREEING - set when HAT is being destroyed - mostly used to detect that
107  *	demap()s can be avoided.
108  *
109  * HAT_VLP - indicates a 32 bit process has a virtual address range less than
110  *	the hardware's physical address range. (VLP->Virtual Less-than Physical)
111  *
112  * HAT_VICTIM - This is set while a hat is being examined for page table
113  *	stealing and prevents it from being freed.
114  *
115  * HAT_SHARED - The hat has exported it's page tables via hat_share()
116  */
117 #define	HAT_FREEING	(0x0001)
118 #define	HAT_VLP		(0x0002)
119 #define	HAT_VICTIM	(0x0004)
120 #define	HAT_SHARED	(0x0008)
121 
122 /*
123  * Additional platform attribute for hat_devload() to force no caching.
124  */
125 #define	HAT_PLAT_NOCACHE	(0x100000)
126 
127 /*
128  * Simple statistics for the HAT. These are just counters that are
129  * atomically incremented. They can be reset directly from the kernel
130  * debugger.
131  */
132 struct hatstats {
133 	uint64_t	hs_reap_attempts;
134 	uint64_t	hs_reaped;
135 	uint64_t	hs_steals;
136 	uint64_t	hs_ptable_allocs;
137 	uint64_t	hs_ptable_frees;
138 	uint64_t	hs_htable_rgets;	/* allocs from reserve */
139 	uint64_t	hs_htable_rputs;	/* putbacks to reserve */
140 	uint64_t	hs_htable_shared;	/* number of htables shared */
141 	uint64_t	hs_htable_unshared;	/* number of htables unshared */
142 	uint64_t	hs_hm_alloc;
143 	uint64_t	hs_hm_free;
144 	uint64_t	hs_hm_put_reserve;
145 	uint64_t	hs_hm_get_reserve;
146 	uint64_t	hs_hm_steals;
147 	uint64_t	hs_hm_steal_exam;
148 };
149 extern struct hatstats hatstat;
150 #define	HATSTAT_INC(x)	(atomic_add_64(&hatstat.x, 1))
151 
152 #if defined(_KERNEL)
153 
154 /*
155  * Useful macro to align hat_XXX() address arguments to a page boundary
156  */
157 #define	ALIGN2PAGE(a)		((uintptr_t)(a) & MMU_PAGEMASK)
158 #define	IS_PAGEALIGNED(a)	(((uintptr_t)(a) & MMU_PAGEOFFSET) == 0)
159 
160 extern uint_t	khat_running;	/* set at end of hat_kern_setup() */
161 extern cpuset_t khat_cpuset;	/* cpuset for kernal address demap Xcalls */
162 extern kmutex_t hat_list_lock;
163 extern kcondvar_t hat_list_cv;
164 
165 
166 
167 /*
168  * Interfaces to setup a cpu private mapping (ie. preemption disabled).
169  * The attr and flags arguments are the same as for hat_devload().
170  * setup() must be called once, then any number of calls to remap(),
171  * followed by a final call to release()
172  *
173  * Used by ppcopy(), page_zero(), the memscrubber, and the kernel debugger.
174  */
175 typedef paddr_t hat_mempte_t;				/* phys addr of PTE */
176 extern hat_mempte_t hat_mempte_setup(caddr_t addr);
177 extern void hat_mempte_remap(pfn_t, caddr_t, hat_mempte_t,
178 	uint_t attr, uint_t flags);
179 extern void hat_mempte_release(caddr_t addr, hat_mempte_t);
180 
181 /*
182  * Interfaces to manage which thread has access to htable and hment reserves.
183  * The USE_HAT_RESERVES macro should always be recomputed in full. Its value
184  * (due to curthread) can change after any call into kmem/vmem.
185  */
186 extern uint_t can_steal_post_boot;
187 extern uint_t use_boot_reserve;
188 extern kthread_t *hat_reserves_thread;
189 #define	USE_HAT_RESERVES()						\
190 	(use_boot_reserve || curthread == hat_reserves_thread ||	\
191 	panicstr != NULL || vmem_is_populator())
192 
193 /*
194  * initialization stuff needed by by startup, mp_startup...
195  */
196 extern void hat_cpu_online(struct cpu *);
197 extern void hat_cpu_offline(struct cpu *);
198 extern void setup_vaddr_for_ppcopy(struct cpu *);
199 extern void teardown_vaddr_for_ppcopy(struct cpu *);
200 extern void clear_boot_mappings(uintptr_t, uintptr_t);
201 
202 /*
203  * magic value to indicate that all TLB entries should be demapped.
204  */
205 #define	DEMAP_ALL_ADDR	(~(uintptr_t)0)
206 
207 /*
208  * not in any include file???
209  */
210 extern void halt(char *fmt);
211 
212 /*
213  * x86 specific routines for use online in setup or i86pc/vm files
214  */
215 extern void hat_kern_alloc(caddr_t segmap_base, size_t segmap_size,
216 	caddr_t ekernelheap);
217 extern void hat_kern_setup(void);
218 extern void hat_tlb_inval(struct hat *hat, uintptr_t va);
219 extern void hat_pte_unmap(htable_t *ht, uint_t entry, uint_t flags,
220 	x86pte_t old_pte, void *pte_ptr);
221 extern void hat_init_finish(void);
222 extern caddr_t hat_kpm_pfn2va(pfn_t pfn);
223 extern pfn_t hat_kpm_va2pfn(caddr_t);
224 extern page_t *hat_kpm_vaddr2page(caddr_t);
225 extern uintptr_t hat_kernelbase(uintptr_t);
226 extern void hat_kmap_init(uintptr_t base, size_t len);
227 
228 extern hment_t *hati_page_unmap(page_t *pp, htable_t *ht, uint_t entry);
229 /*
230  * Hat switch function invoked to load a new context into %cr3
231  */
232 extern void hat_switch(struct hat *hat);
233 
234 #define	pfn_is_foreign(pfn)	__lintzero
235 
236 #endif	/* _KERNEL */
237 
238 #ifdef	__cplusplus
239 }
240 #endif
241 
242 #endif	/* _VM_HAT_I86_H */
243