Merge from vendor branch LIBARCHIVE:
[dragonfly.git] / sys / platform / vkernel / platform / pmap_inval.c
1 /*
2  * Copyright (c) 2003,2004 The DragonFly Project.  All rights reserved.
3  * 
4  * This code is derived from software contributed to The DragonFly Project
5  * by Matthew Dillon <dillon@backplane.com>
6  * 
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in
15  *    the documentation and/or other materials provided with the
16  *    distribution.
17  * 3. Neither the name of The DragonFly Project nor the names of its
18  *    contributors may be used to endorse or promote products derived
19  *    from this software without specific, prior written permission.
20  * 
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
25  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  * 
34  * $DragonFly: src/sys/platform/vkernel/platform/pmap_inval.c,v 1.3 2007/01/08 03:33:43 dillon Exp $
35  */
36
37 /*
38  * pmap invalidation support code.  Certain hardware requirements must
39  * be dealt with when manipulating page table entries and page directory
40  * entries within a pmap.  In particular, we cannot safely manipulate
41  * page tables which are in active use by another cpu (even if it is
42  * running in userland) for two reasons: First, TLB writebacks will
43  * race against our own modifications and tests.  Second, even if we
44  * were to use bus-locked instruction we can still screw up the 
45  * target cpu's instruction pipeline due to Intel cpu errata.
46  */
47
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/kernel.h>
51 #include <sys/proc.h>
52 #include <sys/vmmeter.h>
53 #include <sys/thread2.h>
54
55 #include <sys/mman.h>
56 #include <sys/vmspace.h>
57
58 #include <vm/vm.h>
59 #include <vm/pmap.h>
60 #include <vm/vm_object.h>
61
62 #include <machine/cputypes.h>
63 #include <machine/md_var.h>
64 #include <machine/specialreg.h>
65 #include <machine/smp.h>
66 #include <machine/globaldata.h>
67 #include <machine/pmap.h>
68 #include <machine/pmap_inval.h>
69
70 /*
71  * Initialize for add or flush
72  */
73 void
74 pmap_inval_init(pmap_inval_info_t info)
75 {
76     info->pir_flags = 0;
77 }
78
79 void
80 pmap_inval_add(pmap_inval_info_t info, pmap_t pmap, vm_offset_t va)
81 {
82     if (pmap == &kernel_pmap) {
83         madvise((void *)va, PAGE_SIZE, MADV_INVAL);
84     } else {
85         vmspace_mcontrol(pmap, (void *)va, PAGE_SIZE, MADV_INVAL, 0);
86     }
87 }
88
89 void
90 pmap_inval_flush(pmap_inval_info_t info)
91 {
92     info->pir_flags = 0;
93 }
94
95 #if 0
96
97 /*
98  * Add a (pmap, va) pair to the invalidation list and protect access
99  * as appropriate.
100  */
101 void
102 pmap_inval_add(pmap_inval_info_t info, pmap_t pmap, vm_offset_t va)
103 {
104 #ifdef SMP
105     if ((info->pir_flags & PIRF_CPUSYNC) == 0) {
106         info->pir_flags |= PIRF_CPUSYNC;
107         info->pir_cpusync.cs_run_func = NULL;
108         info->pir_cpusync.cs_fin1_func = NULL;
109         info->pir_cpusync.cs_fin2_func = NULL;
110         lwkt_cpusync_start(pmap->pm_active, &info->pir_cpusync);
111     } else if (pmap->pm_active & ~info->pir_cpusync.cs_mask) {
112         lwkt_cpusync_add(pmap->pm_active, &info->pir_cpusync);
113     }
114 #else
115     if (pmap->pm_active == 0)
116         return;
117 #endif
118     if ((info->pir_flags & (PIRF_INVLTLB|PIRF_INVL1PG)) == 0) {
119         if (va == (vm_offset_t)-1) {
120             info->pir_flags |= PIRF_INVLTLB;
121 #ifdef SMP
122             info->pir_cpusync.cs_fin2_func = _cpu_invltlb;
123 #endif
124         } else {
125             info->pir_flags |= PIRF_INVL1PG;
126             info->pir_cpusync.cs_data = (void *)va;
127 #ifdef SMP
128             info->pir_cpusync.cs_fin2_func = _cpu_invl1pg;
129 #endif
130         }
131     } else {
132         info->pir_flags |= PIRF_INVLTLB;
133 #ifdef SMP
134         info->pir_cpusync.cs_fin2_func = _cpu_invltlb;
135 #endif
136     }
137 }
138
139 /*
140  * Synchronize changes with target cpus.
141  */
142 void
143 pmap_inval_flush(pmap_inval_info_t info)
144 {
145 #ifdef SMP
146     if (info->pir_flags & PIRF_CPUSYNC)
147         lwkt_cpusync_finish(&info->pir_cpusync);
148 #else
149     if (info->pir_flags & PIRF_INVLTLB)
150         _cpu_invltlb(NULL);
151     else if (info->pir_flags & PIRF_INVL1PG)
152         _cpu_invl1pg(info->pir_cpusync.cs_data);
153 #endif
154     info->pir_flags = 0;
155 }
156
157 #endif