]> git.saurik.com Git - apple/xnu.git/blob - osfmk/i386/phys.c
xnu-792.25.20.tar.gz
[apple/xnu.git] / osfmk / i386 / phys.c
1 /*
2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
11 *
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22 /*
23 * @OSF_COPYRIGHT@
24 */
25 /*
26 * Mach Operating System
27 * Copyright (c) 1991,1990,1989 Carnegie Mellon University
28 * All Rights Reserved.
29 *
30 * Permission to use, copy, modify and distribute this software and its
31 * documentation is hereby granted, provided that both the copyright
32 * notice and this permission notice appear in all copies of the
33 * software, derivative works or modified versions, and any portions
34 * thereof, and that both notices appear in supporting documentation.
35 *
36 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
37 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
38 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
39 *
40 * Carnegie Mellon requests users of this software to return to
41 *
42 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
43 * School of Computer Science
44 * Carnegie Mellon University
45 * Pittsburgh PA 15213-3890
46 *
47 * any improvements or extensions that they make and grant Carnegie Mellon
48 * the rights to redistribute these changes.
49 */
50
51 #include <mach_rt.h>
52 #include <mach_debug.h>
53 #include <mach_ldebug.h>
54
55 #include <sys/kdebug.h>
56
57 #include <mach/kern_return.h>
58 #include <mach/thread_status.h>
59 #include <mach/vm_param.h>
60
61 #include <kern/counters.h>
62 #include <kern/mach_param.h>
63 #include <kern/task.h>
64 #include <kern/thread.h>
65 #include <kern/sched_prim.h>
66 #include <kern/misc_protos.h>
67 #include <kern/assert.h>
68 #include <kern/spl.h>
69 #include <ipc/ipc_port.h>
70 #include <vm/vm_kern.h>
71 #include <vm/vm_map.h>
72 #include <vm/pmap.h>
73
74 #include <i386/cpu_data.h>
75 #include <i386/cpu_number.h>
76 #include <i386/thread.h>
77 #include <i386/eflags.h>
78 #include <i386/proc_reg.h>
79 #include <i386/seg.h>
80 #include <i386/tss.h>
81 #include <i386/user_ldt.h>
82 #include <i386/fpu.h>
83 #include <i386/iopb_entries.h>
84 #include <i386/misc_protos.h>
85
86 /*
87 * pmap_zero_page zeros the specified (machine independent) page.
88 */
89 void
90 pmap_zero_page(
91 ppnum_t pn)
92 {
93 assert(pn != vm_page_fictitious_addr);
94 bzero_phys((addr64_t)i386_ptob(pn), PAGE_SIZE);
95 }
96
97 /*
98 * pmap_zero_part_page
99 * zeros the specified (machine independent) part of a page.
100 */
101 void
102 pmap_zero_part_page(
103 ppnum_t pn,
104 vm_offset_t offset,
105 vm_size_t len)
106 {
107 assert(pn != vm_page_fictitious_addr);
108 assert(offset + len <= PAGE_SIZE);
109 bzero_phys((addr64_t)(i386_ptob(pn) + offset), len);
110 }
111
112 /*
113 * pmap_copy_page copies the specified (machine independent) pages.
114 */
115 void
116 pmap_copy_part_page(
117 ppnum_t psrc,
118 vm_offset_t src_offset,
119 ppnum_t pdst,
120 vm_offset_t dst_offset,
121 vm_size_t len)
122 {
123 pmap_paddr_t src, dst;
124
125 assert(psrc != vm_page_fictitious_addr);
126 assert(pdst != vm_page_fictitious_addr);
127
128 src = i386_ptob(psrc);
129 dst = i386_ptob(pdst);
130
131 assert((((uint32_t)dst & PAGE_MASK) + dst_offset + len) <= PAGE_SIZE);
132 assert((((uint32_t)src & PAGE_MASK) + src_offset + len) <= PAGE_SIZE);
133
134 bcopy_phys((addr64_t)src + (src_offset & INTEL_OFFMASK),
135 (addr64_t)dst + (dst_offset & INTEL_OFFMASK),
136 len);
137 }
138
139 /*
140 * pmap_copy_part_lpage copies part of a virtually addressed page
141 * to a physically addressed page.
142 */
143 void
144 pmap_copy_part_lpage(
145 vm_offset_t src,
146 ppnum_t pdst,
147 vm_offset_t dst_offset,
148 vm_size_t len)
149 {
150 mapwindow_t *map;
151
152 assert(pdst != vm_page_fictitious_addr);
153 assert((dst_offset + len) <= PAGE_SIZE);
154
155 mp_disable_preemption();
156
157 map = pmap_get_mapwindow(INTEL_PTE_VALID | INTEL_PTE_RW | (i386_ptob(pdst) & PG_FRAME) |
158 INTEL_PTE_REF | INTEL_PTE_MOD);
159 if (map == 0) {
160 panic("pmap_copy_part_lpage");
161 }
162 invlpg((uintptr_t)map->prv_CADDR);
163
164 memcpy((void *) (map->prv_CADDR + (dst_offset & INTEL_OFFMASK)), (void *) src, len);
165 *map->prv_CMAP = 0;
166
167 mp_enable_preemption();
168 }
169
170 /*
171 * pmap_copy_part_rpage copies part of a physically addressed page
172 * to a virtually addressed page.
173 */
174 void
175 pmap_copy_part_rpage(
176 ppnum_t psrc,
177 vm_offset_t src_offset,
178 vm_offset_t dst,
179 vm_size_t len)
180 {
181 mapwindow_t *map;
182
183 assert(psrc != vm_page_fictitious_addr);
184 assert((src_offset + len) <= PAGE_SIZE);
185
186 mp_disable_preemption();
187
188 map = pmap_get_mapwindow(INTEL_PTE_VALID | INTEL_PTE_RW | (i386_ptob(psrc) & PG_FRAME) |
189 INTEL_PTE_REF);
190 if (map == 0) {
191 panic("pmap_copy_part_rpage");
192 }
193 invlpg((uintptr_t) map->prv_CADDR);
194
195 memcpy((void *) dst, (void *) (map->prv_CADDR + (src_offset & INTEL_OFFMASK)), len);
196 *map->prv_CMAP = 0;
197
198 mp_enable_preemption();
199 }
200
201 /*
202 * kvtophys(addr)
203 *
204 * Convert a kernel virtual address to a physical address
205 */
206 addr64_t
207 kvtophys(
208 vm_offset_t addr)
209 {
210 pt_entry_t *ptep;
211 pmap_paddr_t pa;
212
213 mp_disable_preemption();
214 if ((ptep = pmap_pte(kernel_pmap, (vm_map_offset_t)addr)) == PT_ENTRY_NULL) {
215 pa = 0;
216 } else {
217 pa = pte_to_pa(*ptep) | (addr & INTEL_OFFMASK);
218 }
219 mp_enable_preemption_no_check();
220 return ((addr64_t)pa);
221 }
222