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