xref: /netbsd/sys/arch/sun2/sun2/control.c (revision bf9ec67e)
1 /*	$NetBSD: control.c,v 1.2 2001/11/30 18:06:55 fredette Exp $	*/
2 
3 /*-
4  * Copyright (c) 1996 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Adam Glass, Gordon W. Ross, and Matthew Fredette.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *        This product includes software developed by the NetBSD
21  *        Foundation, Inc. and its contributors.
22  * 4. Neither the name of The NetBSD Foundation nor the names of its
23  *    contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 #include <sys/param.h>
40 
41 #include <machine/pte.h>
42 #include <sun2/sun2/control.h>
43 
44 int
45 get_context()
46 {
47 	return (get_control_byte(CONTEXT_REG) & CONTEXT_MASK);
48 }
49 
50 void
51 set_context(c)
52 	int c;
53 {
54 	set_control_byte(CONTEXT_REG, (c & CONTEXT_MASK));
55 }
56 
57 u_int
58 get_pte(va)
59 	vaddr_t va;
60 {
61 	u_int pte;
62 
63 	pte = get_control_word(CONTROL_ADDR_BUILD(PGMAP_BASE, va));
64 	if (pte & PG_VALID) {
65 		/*
66 		 * This clears bit 30 (the kernel readable bit, which
67 		 * should always be set), bit 28 (which should always
68 		 * be set), bit 26 (the user writable bit, which we
69 		 * always have tracking the kernel writable bit), and
70 		 * bit 25 (the fill-on-demand bit, which should always
71 		 * be set).  In the protection, this leaves bit 29
72 		 * (the kernel writable bit) and bit 27 (the user
73 		 * readable bit).  See pte.h for more about this
74 		 * hack.
75 		 */
76 		pte &= ~(0x56000000);
77 		/*
78 		 * Flip bit 27 (the user readable bit) to become bit
79 		 * 27 (the PG_SYSTEM bit).
80 		 */
81 		pte ^= (PG_SYSTEM);
82 	}
83 	return (pte);
84 }
85 
86 void
87 set_pte(va, pte)
88 	vaddr_t va;
89 	u_int pte;
90 {
91 	if (pte & PG_VALID) {
92 		/* Clear bit 26 (the user writable bit).  */
93 		pte &= (~0x04000000);
94 		/*
95 		 * Flip bit 27 (the PG_SYSTEM bit) to become bit 27
96 		 * (the user readable bit).
97 		 */
98 		pte ^= (PG_SYSTEM);
99 		/*
100 		 * Always set bits 30 (the kernel readable bit), bit
101 		 * 28, and bit 25 (the fill-on-demand bit), and set
102 		 * bit 26 (the user writable bit) iff bit 29 (the
103 		 * kernel writable bit) is set *and* bit 27 (the user
104 		 * readable bit) is set.  This latter bit of logic is
105 		 * expressed in the bizarre second term below, chosen
106 		 * because it needs no branches.
107 		 */
108 #if (PG_WRITE >> 2) != PG_SYSTEM
109 #error	"PG_WRITE and PG_SYSTEM definitions don't match!"
110 #endif
111 		pte |= 0x52000000
112 		    | ((((pte & PG_WRITE) >> 2) & pte) >> 1);
113 	}
114 	set_control_word(CONTROL_ADDR_BUILD(PGMAP_BASE, va), pte);
115 }
116 
117 int
118 get_segmap(va)
119 	vaddr_t va;
120 {
121 	return (get_control_byte(CONTROL_ADDR_BUILD(SEGMAP_BASE, va)));
122 }
123 
124 void
125 set_segmap(va, sme)
126 	vaddr_t va;
127 	int sme;
128 {
129 	set_control_byte(CONTROL_ADDR_BUILD(SEGMAP_BASE, va), sme);
130 }
131