xref: /netbsd-src/sys/arch/arm/arm32/sys_machdep.c (revision df27e1ef9c38ce5be82705990191145a274de93f)
1 /*	$NetBSD: sys_machdep.c,v 1.26 2020/06/20 15:45:22 skrll Exp $	*/
2 
3 /*
4  * Copyright (c) 1995-1997 Mark Brinicombe.
5  * All rights reserved.
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  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by Mark Brinicombe
18  * 4. The name of the company nor the name of the author may be used to
19  *    endorse or promote products derived from this software without specific
20  *    prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
23  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
24  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25  * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
26  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
27  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
28  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  * RiscBSD kernel project
35  *
36  * sys_machdep.c
37  *
38  * Machine dependent syscalls
39  *
40  * Created      : 10/01/96
41  */
42 
43 #include <sys/cdefs.h>
44 __KERNEL_RCSID(0, "$NetBSD: sys_machdep.c,v 1.26 2020/06/20 15:45:22 skrll Exp $");
45 
46 #include <sys/param.h>
47 
48 #include <sys/cpu.h>
49 #include <sys/proc.h>
50 #include <sys/syscallargs.h>
51 #include <sys/sysctl.h>
52 #include <sys/systm.h>
53 
54 #include <uvm/uvm_extern.h>
55 
56 #include <machine/sysarch.h>
57 #include <machine/pcb.h>
58 #include <arm/vfpreg.h>
59 #include <arm/locore.h>
60 
61 /* Prototypes */
62 static int arm32_sync_icache(struct lwp *, const void *, register_t *);
63 static int arm32_drain_writebuf(struct lwp *, const void *, register_t *);
64 static int arm32_vfp_fpscr(struct lwp *, const void *, register_t *);
65 static int arm32_fpu_used(struct lwp *, const void *, register_t *);
66 
67 static int
arm32_sync_icache(struct lwp * l,const void * args,register_t * retval)68 arm32_sync_icache(struct lwp *l, const void *args, register_t *retval)
69 {
70 	struct arm_sync_icache_args ua;
71 	int error;
72 
73 	if ((error = copyin(args, &ua, sizeof(ua))) != 0)
74 		return error;
75 
76 	pmap_icache_sync_range(vm_map_pmap(&l->l_proc->p_vmspace->vm_map),
77 	    ua.addr, ua.addr + ua.len);
78 
79 	*retval = 0;
80 	return 0;
81 }
82 
83 static int
arm32_drain_writebuf(struct lwp * l,const void * args,register_t * retval)84 arm32_drain_writebuf(struct lwp *l, const void *args, register_t *retval)
85 {
86 	/* No args. */
87 
88 	cpu_drain_writebuf();
89 
90 	*retval = 0;
91 	return 0;
92 }
93 
94 static int
arm32_vfp_fpscr(struct lwp * l,const void * uap,register_t * retval)95 arm32_vfp_fpscr(struct lwp *l, const void *uap, register_t *retval)
96 {
97 	struct pcb * const pcb = lwp_getpcb(l);
98 
99 #ifdef FPU_VFP
100 	/*
101 	 * Save the current VFP state (to make sure the FPSCR copy is
102 	 * up to date).
103 	 */
104 	vfp_savecontext(l);
105 #endif
106 
107 	retval[0] = pcb->pcb_vfp.vfp_fpscr;
108 	if (uap) {
109 		extern uint32_t vfp_fpscr_changable;
110 		struct arm_vfp_fpscr_args ua;
111 		int error;
112 		if ((error = copyin(uap, &ua, sizeof(ua))) != 0)
113 			return error;
114 		if ((ua.fpscr_clear|ua.fpscr_set) & ~vfp_fpscr_changable)
115 			return EINVAL;
116 		pcb->pcb_vfp.vfp_fpscr &= ~ua.fpscr_clear;
117 		pcb->pcb_vfp.vfp_fpscr |= ua.fpscr_set;
118 	}
119 
120 	return 0;
121 }
122 
123 static int
arm32_fpu_used(struct lwp * l,const void * uap,register_t * retval)124 arm32_fpu_used(struct lwp *l, const void *uap, register_t *retval)
125 {
126 	/* No args */
127 #ifdef FPU_VFP
128 	retval[0] = vfp_used_p(l);
129 #else
130 	retval[0] = false;
131 #endif
132 	return 0;
133 }
134 
135 int
sys_sysarch(struct lwp * l,const struct sys_sysarch_args * uap,register_t * retval)136 sys_sysarch(struct lwp *l, const struct sys_sysarch_args *uap, register_t *retval)
137 {
138 	/* {
139 		syscallarg(int) op;
140 		syscallarg(void *) parms;
141 	} */
142 	int error = 0;
143 
144 	switch(SCARG(uap, op)) {
145 	case ARM_SYNC_ICACHE :
146 		error = arm32_sync_icache(l, SCARG(uap, parms), retval);
147 		break;
148 
149 	case ARM_DRAIN_WRITEBUF :
150 		error = arm32_drain_writebuf(l, SCARG(uap, parms), retval);
151 		break;
152 
153 	case ARM_VFP_FPSCR :
154 		error = arm32_vfp_fpscr(l, SCARG(uap, parms), retval);
155 		break;
156 
157 	case ARM_FPU_USED :
158 		error = arm32_fpu_used(l, SCARG(uap, parms), retval);
159 		break;
160 
161 	default:
162 		error = EINVAL;
163 		break;
164 	}
165 	return error;
166 }
167 
168 int
cpu_lwp_setprivate(lwp_t * l,void * addr)169 cpu_lwp_setprivate(lwp_t *l, void *addr)
170 {
171 #ifdef _ARM_ARCH_6
172 	if (l == curlwp) {
173 		u_int val = (u_int)addr;
174 		kpreempt_disable();
175 		armreg_tpidruro_write(val);
176 		kpreempt_enable();
177 	}
178 	return 0;
179 #else
180 	return 0;
181 #endif
182 }
183