macros.h revision 1.37 1 1.37 matt /* $NetBSD: macros.h,v 1.37 2006/07/08 00:25:24 matt Exp $ */
2 1.2 cgd
3 1.1 ragge /*
4 1.18 ragge * Copyright (c) 1994, 1998, 2000 Ludd, University of Lule}, Sweden.
5 1.1 ragge * All rights reserved.
6 1.1 ragge *
7 1.1 ragge * Redistribution and use in source and binary forms, with or without
8 1.1 ragge * modification, are permitted provided that the following conditions
9 1.1 ragge * are met:
10 1.1 ragge * 1. Redistributions of source code must retain the above copyright
11 1.1 ragge * notice, this list of conditions and the following disclaimer.
12 1.1 ragge * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 ragge * notice, this list of conditions and the following disclaimer in the
14 1.1 ragge * documentation and/or other materials provided with the distribution.
15 1.1 ragge * 3. All advertising materials mentioning features or use of this software
16 1.1 ragge * must display the following acknowledgement:
17 1.1 ragge * This product includes software developed at Ludd, University of Lule}.
18 1.1 ragge * 4. The name of the author may not be used to endorse or promote products
19 1.1 ragge * derived from this software without specific prior written permission
20 1.1 ragge *
21 1.1 ragge * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 1.1 ragge * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 1.1 ragge * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 1.1 ragge * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 1.1 ragge * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 1.1 ragge * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 1.1 ragge * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 1.1 ragge * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 1.1 ragge * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 1.1 ragge * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 1.1 ragge */
32 1.1 ragge
33 1.1 ragge /* All bugs are subject to removal without further notice */
34 1.3 ragge
35 1.31 kleink #if !defined(_VAX_MACROS_H_) && !defined(__lint__)
36 1.15 ragge #define _VAX_MACROS_H_
37 1.1 ragge
38 1.29 ragge void __blkset(void *, int, size_t);
39 1.29 ragge void __blkcpy(const void *, void *, size_t);
40 1.29 ragge
41 1.1 ragge /* Here general macros are supposed to be stored */
42 1.1 ragge
43 1.36 perry static __inline int __attribute__((__unused__))
44 1.37 matt vax_ffs(int reg)
45 1.16 ragge {
46 1.3 ragge register int val;
47 1.3 ragge
48 1.35 perry __asm volatile ("ffs $0,$32,%1,%0;"
49 1.25 matt "bneq 1f;"
50 1.25 matt "mnegl $1,%0;"
51 1.25 matt "1:;"
52 1.25 matt "incl %0"
53 1.19 matt : "=&r" (val)
54 1.3 ragge : "r" (reg) );
55 1.3 ragge return val;
56 1.3 ragge }
57 1.37 matt #define ffs vax_ffs
58 1.3 ragge
59 1.36 perry static __inline void __attribute__((__unused__))
60 1.37 matt vax_remque(void *p)
61 1.16 ragge {
62 1.35 perry __asm volatile ("remque (%0),%0;clrl 4(%0)"
63 1.3 ragge :
64 1.3 ragge : "r" (p)
65 1.3 ragge : "memory" );
66 1.3 ragge }
67 1.37 matt #define _remque vax_remque
68 1.3 ragge
69 1.36 perry static __inline void __attribute__((__unused__))
70 1.37 matt vax_insque(void *p, void *q)
71 1.16 ragge {
72 1.35 perry __asm volatile ("insque (%0),(%1)"
73 1.15 ragge :
74 1.15 ragge : "r" (p),"r" (q)
75 1.15 ragge : "memory" );
76 1.3 ragge }
77 1.37 matt #define _insque vax_insque
78 1.3 ragge
79 1.37 matt #if 0
80 1.36 perry static __inline void * __attribute__((__unused__))
81 1.37 matt vax_memcpy(void *to, const void *from, size_t len)
82 1.16 ragge {
83 1.29 ragge if (len > 65535) {
84 1.29 ragge __blkcpy(from, to, len);
85 1.29 ragge } else {
86 1.37 matt __asm volatile ("movc3 %1,(%2),%0"
87 1.37 matt : "=m" (*(char *)to)
88 1.37 matt : "g" (len), "r" (*(const char *)from)
89 1.16 ragge :"r0","r1","r2","r3","r4","r5","memory","cc");
90 1.29 ragge }
91 1.22 matt return to;
92 1.16 ragge }
93 1.37 matt #define memcpy vax_memcpy
94 1.37 matt
95 1.36 perry static __inline void * __attribute__((__unused__))
96 1.37 matt vax_memmove(void *to, const void *from, size_t len)
97 1.16 ragge {
98 1.29 ragge if (len > 65535) {
99 1.29 ragge __blkcpy(from, to, len);
100 1.29 ragge } else {
101 1.37 matt __asm volatile ("movc3 %1,%2,%0"
102 1.37 matt : "=m" (*(char *)to)
103 1.37 matt : "g" (len), "mo" (*(const char *)from)
104 1.16 ragge :"r0","r1","r2","r3","r4","r5","memory","cc");
105 1.29 ragge }
106 1.22 matt return to;
107 1.16 ragge }
108 1.37 matt #define memmove vax_memmove
109 1.29 ragge #endif
110 1.15 ragge
111 1.36 perry static __inline void * __attribute__((__unused__))
112 1.37 matt vax_memset(void *block, int c, size_t len)
113 1.16 ragge {
114 1.29 ragge if (len > 65535) {
115 1.24 thorpej __blkset(block, c, len);
116 1.29 ragge } else {
117 1.35 perry __asm volatile ("movc5 $0,(%%sp),%2,%1,%0"
118 1.37 matt : "=m" (*(char *)block)
119 1.37 matt : "g" (len), "g" (c)
120 1.16 ragge :"r0","r1","r2","r3","r4","r5","memory","cc");
121 1.16 ragge }
122 1.16 ragge return block;
123 1.16 ragge }
124 1.37 matt #define memset vax_memset
125 1.3 ragge
126 1.29 ragge #ifdef notdef
127 1.16 ragge /* XXX - the return syntax of memcmp is wrong */
128 1.36 perry static __inline int __attribute__((__unused__))
129 1.16 ragge memcmp(const void *b1, const void *b2, size_t len)
130 1.16 ragge {
131 1.17 ragge register int ret;
132 1.16 ragge
133 1.35 perry __asm volatile("cmpc3 %3,(%1),(%2);"
134 1.25 matt "movl %%r0,%0"
135 1.16 ragge : "=r" (ret)
136 1.16 ragge : "r" (b1), "r" (b2), "r" (len)
137 1.16 ragge : "r0","r1","r2","r3" );
138 1.16 ragge return ret;
139 1.16 ragge }
140 1.16 ragge
141 1.36 perry static __inline int __attribute__((__unused__))
142 1.16 ragge bcmp(const void *b1, const void *b2, size_t len)
143 1.16 ragge {
144 1.17 ragge register int ret;
145 1.3 ragge
146 1.35 perry __asm volatile("cmpc3 %3,(%1),(%2);"
147 1.25 matt "movl %%r0,%0"
148 1.3 ragge : "=r" (ret)
149 1.3 ragge : "r" (b1), "r" (b2), "r" (len)
150 1.3 ragge : "r0","r1","r2","r3" );
151 1.3 ragge return ret;
152 1.3 ragge }
153 1.3 ragge
154 1.16 ragge /* Begin nya */
155 1.36 perry static __inline size_t __attribute__((__unused__))
156 1.16 ragge strlen(const char *cp)
157 1.16 ragge {
158 1.17 ragge register size_t ret;
159 1.16 ragge
160 1.35 perry __asm volatile("locc $0,$65535,(%1);"
161 1.25 matt "subl3 %%r0,$65535,%0"
162 1.16 ragge : "=r" (ret)
163 1.16 ragge : "r" (cp)
164 1.16 ragge : "r0","r1","cc" );
165 1.16 ragge return ret;
166 1.16 ragge }
167 1.16 ragge
168 1.36 perry static __inline char * __attribute__((__unused__))
169 1.16 ragge strcat(char *cp, const char *c2)
170 1.16 ragge {
171 1.35 perry __asm volatile("locc $0,$65535,(%1);"
172 1.25 matt "subl3 %%r0,$65535,%%r2;"
173 1.25 matt "incl %%r2;"
174 1.25 matt "locc $0,$65535,(%0);"
175 1.25 matt "movc3 %%r2,(%1),(%%r1)"
176 1.16 ragge :
177 1.16 ragge : "r" (cp), "r" (c2)
178 1.16 ragge : "r0","r1","r2","r3","r4","r5","memory","cc");
179 1.16 ragge return cp;
180 1.16 ragge }
181 1.16 ragge
182 1.36 perry static __inline char * __attribute__((__unused__))
183 1.16 ragge strncat(char *cp, const char *c2, size_t count)
184 1.16 ragge {
185 1.35 perry __asm volatile("locc $0,%2,(%1);"
186 1.28 thorpej "subl3 %%r0,%2,%%r2;"
187 1.25 matt "locc $0,$65535,(%0);"
188 1.25 matt "movc3 %%r2,(%1),(%%r1);"
189 1.25 matt "movb $0,(%%r3)"
190 1.16 ragge :
191 1.16 ragge : "r" (cp), "r" (c2), "g"(count)
192 1.16 ragge : "r0","r1","r2","r3","r4","r5","memory","cc");
193 1.16 ragge return cp;
194 1.16 ragge }
195 1.16 ragge
196 1.36 perry static __inline char * __attribute__((__unused__))
197 1.16 ragge strcpy(char *cp, const char *c2)
198 1.16 ragge {
199 1.35 perry __asm volatile("locc $0,$65535,(%1);"
200 1.25 matt "subl3 %%r0,$65535,%%r2;"
201 1.25 matt "movc3 %%r2,(%1),(%0);"
202 1.25 matt "movb $0,(%%r3)"
203 1.16 ragge :
204 1.16 ragge : "r" (cp), "r" (c2)
205 1.16 ragge : "r0","r1","r2","r3","r4","r5","memory","cc");
206 1.16 ragge return cp;
207 1.16 ragge }
208 1.16 ragge
209 1.36 perry static __inline char * __attribute__((__unused__))
210 1.16 ragge strncpy(char *cp, const char *c2, size_t len)
211 1.16 ragge {
212 1.35 perry __asm volatile("movl %2,%%r2;"
213 1.25 matt "locc $0,%%r2,(%1);"
214 1.25 matt "beql 1f;"
215 1.25 matt "subl3 %%r0,%2,%%r2;"
216 1.25 matt "clrb (%0)[%%r2];"
217 1.25 matt "1:;"
218 1.25 matt "movc3 %%r2,(%1),(%0)"
219 1.16 ragge :
220 1.16 ragge : "r" (cp), "r" (c2), "g"(len)
221 1.16 ragge : "r0","r1","r2","r3","r4","r5","memory","cc");
222 1.16 ragge return cp;
223 1.16 ragge }
224 1.16 ragge
225 1.36 perry static __inline void * __attribute__((__unused__))
226 1.16 ragge memchr(const void *cp, int c, size_t len)
227 1.16 ragge {
228 1.16 ragge void *ret;
229 1.35 perry __asm volatile("locc %2,%3,(%1);"
230 1.25 matt "bneq 1f;"
231 1.25 matt "clrl %%r1;"
232 1.25 matt "1:;"
233 1.25 matt "movl %%r1,%0"
234 1.16 ragge : "=g"(ret)
235 1.16 ragge : "r" (cp), "r" (c), "g"(len)
236 1.16 ragge : "r0","r1","cc");
237 1.16 ragge return ret;
238 1.16 ragge }
239 1.16 ragge
240 1.36 perry static __inline int __attribute__((__unused__))
241 1.16 ragge strcmp(const char *cp, const char *c2)
242 1.16 ragge {
243 1.17 ragge register int ret;
244 1.35 perry __asm volatile("locc $0,$65535,(%1);"
245 1.25 matt "subl3 %%r0,$65535,%%r0;"
246 1.25 matt "incl %%r0;"
247 1.25 matt "cmpc3 %%r0,(%1),(%2);"
248 1.25 matt "beql 1f;"
249 1.25 matt "movl $1,%%r2;"
250 1.25 matt "cmpb (%%r1),(%%r3);"
251 1.25 matt "bcc 1f;"
252 1.25 matt "mnegl $1,%%r2;"
253 1.25 matt "1:;"
254 1.25 matt "movl %%r2,%0"
255 1.16 ragge : "=g"(ret)
256 1.16 ragge : "r" (cp), "r" (c2)
257 1.16 ragge : "r0","r1","r2","r3","cc");
258 1.16 ragge return ret;
259 1.16 ragge }
260 1.29 ragge #endif
261 1.16 ragge
262 1.9 cgd #if 0 /* unused, but no point in deleting it since it _is_ an instruction */
263 1.36 perry static __inline int __attribute__((__unused__))
264 1.24 thorpej locc(int mask, char *cp, size_t size){
265 1.3 ragge register ret;
266 1.3 ragge
267 1.35 perry __asm volatile("locc %1,%2,(%3);"
268 1.25 matt "movl %%r0,%0"
269 1.3 ragge : "=r" (ret)
270 1.3 ragge : "r" (mask),"r"(size),"r"(cp)
271 1.3 ragge : "r0","r1" );
272 1.3 ragge return ret;
273 1.3 ragge }
274 1.9 cgd #endif
275 1.3 ragge
276 1.36 perry static __inline int __attribute__((__unused__))
277 1.37 matt vax_scanc(u_int size, const u_char *cp, const u_char *table, int mask)
278 1.16 ragge {
279 1.17 ragge register int ret;
280 1.3 ragge
281 1.35 perry __asm volatile("scanc %1,(%2),(%3),%4;"
282 1.25 matt "movl %%r0,%0"
283 1.3 ragge : "=g"(ret)
284 1.3 ragge : "r"(size),"r"(cp),"r"(table),"r"(mask)
285 1.3 ragge : "r0","r1","r2","r3" );
286 1.3 ragge return ret;
287 1.3 ragge }
288 1.37 matt #define scanc vax_scanc
289 1.3 ragge
290 1.36 perry static __inline int __attribute__((__unused__))
291 1.37 matt vax_skpc(int mask, size_t size, u_char *cp)
292 1.16 ragge {
293 1.17 ragge register int ret;
294 1.3 ragge
295 1.35 perry __asm volatile("skpc %1,%2,(%3);"
296 1.25 matt "movl %%r0,%0"
297 1.3 ragge : "=g"(ret)
298 1.3 ragge : "r"(mask),"r"(size),"r"(cp)
299 1.3 ragge : "r0","r1" );
300 1.3 ragge return ret;
301 1.23 ragge }
302 1.37 matt #define skpc vax_skpc
303 1.23 ragge
304 1.23 ragge /*
305 1.23 ragge * Set/clear a bit at a memory position; interlocked.
306 1.23 ragge * Return 0 if already set, 1 otherwise.
307 1.23 ragge */
308 1.36 perry static __inline int __attribute__((__unused__))
309 1.23 ragge bbssi(int bitnr, long *addr)
310 1.23 ragge {
311 1.23 ragge register int ret;
312 1.23 ragge
313 1.35 perry __asm volatile("clrl %%r0;"
314 1.25 matt "bbssi %1,%2,1f;"
315 1.25 matt "incl %%r0;"
316 1.25 matt "1:;"
317 1.25 matt "movl %%r0,%0"
318 1.23 ragge : "=&r"(ret)
319 1.23 ragge : "g"(bitnr),"m"(*addr)
320 1.23 ragge : "r0","cc","memory");
321 1.23 ragge return ret;
322 1.23 ragge }
323 1.23 ragge
324 1.36 perry static __inline int __attribute__((__unused__))
325 1.23 ragge bbcci(int bitnr, long *addr)
326 1.23 ragge {
327 1.23 ragge register int ret;
328 1.23 ragge
329 1.35 perry __asm volatile("clrl %%r0;"
330 1.25 matt "bbcci %1,%2,1f;"
331 1.25 matt "incl %%r0;"
332 1.25 matt "1:;"
333 1.25 matt "movl %%r0,%0"
334 1.23 ragge : "=&r"(ret)
335 1.23 ragge : "g"(bitnr),"m"(*addr)
336 1.23 ragge : "r0","cc","memory");
337 1.23 ragge return ret;
338 1.3 ragge }
339 1.3 ragge
340 1.15 ragge #define setrunqueue(p) \
341 1.35 perry __asm volatile("movl %0,%%r0;jsb Setrq" :: "g"(p):"r0","r1","r2")
342 1.12 ragge
343 1.15 ragge #define remrunqueue(p) \
344 1.35 perry __asm volatile("movl %0,%%r0;jsb Remrq" :: "g"(p):"r0","r1","r2")
345 1.12 ragge
346 1.28 thorpej #define cpu_switch(p, newp) ({ \
347 1.28 thorpej register int ret; \
348 1.35 perry __asm volatile("movpsl -(%%sp);jsb Swtch; movl %%r0,%0" \
349 1.28 thorpej : "=g"(ret) ::"r0","r1","r2","r3","r4","r5"); \
350 1.28 thorpej ret; })
351 1.28 thorpej
352 1.28 thorpej #define cpu_switchto(p, newp) \
353 1.34 perry __asm volatile("movpsl -(%%sp); movl %0,%%r2; jsb Swtchto" \
354 1.28 thorpej :: "g" (newp) : "r0", "r1", "r2", "r3", "r4", "r5")
355 1.18 ragge
356 1.18 ragge /*
357 1.18 ragge * Interlock instructions. Used both in multiprocessor environments to
358 1.18 ragge * lock between CPUs and in uniprocessor systems when locking is required
359 1.18 ragge * between I/O devices and the master CPU.
360 1.18 ragge */
361 1.18 ragge /*
362 1.18 ragge * Insqti() locks and inserts an element into the end of a queue.
363 1.18 ragge * Returns -1 if interlock failed, 1 if inserted OK and 0 if first in queue.
364 1.18 ragge */
365 1.36 perry static __inline int __attribute__((__unused__))
366 1.18 ragge insqti(void *entry, void *header) {
367 1.18 ragge register int ret;
368 1.18 ragge
369 1.35 perry __asm volatile(
370 1.26 matt " mnegl $1,%0;"
371 1.26 matt " insqti (%1),(%2);"
372 1.26 matt " bcs 1f;" /* failed insert */
373 1.26 matt " beql 2f;" /* jump if first entry */
374 1.26 matt " movl $1,%0;"
375 1.26 matt " brb 1f;"
376 1.26 matt "2: clrl %0;"
377 1.26 matt " 1:;"
378 1.19 matt : "=&g"(ret)
379 1.18 ragge : "r"(entry), "r"(header)
380 1.18 ragge : "memory");
381 1.18 ragge
382 1.18 ragge return ret;
383 1.18 ragge }
384 1.18 ragge
385 1.18 ragge /*
386 1.18 ragge * Remqhi() removes an element from the head of the queue.
387 1.18 ragge * Returns -1 if interlock failed, 0 if queue empty, address of the
388 1.18 ragge * removed element otherwise.
389 1.18 ragge */
390 1.36 perry static __inline void * __attribute__((__unused__))
391 1.18 ragge remqhi(void *header) {
392 1.18 ragge register void *ret;
393 1.18 ragge
394 1.35 perry __asm volatile(
395 1.26 matt " remqhi (%1),%0;"
396 1.26 matt " bcs 1f;" /* failed interlock */
397 1.26 matt " bvs 2f;" /* nothing was removed */
398 1.26 matt " brb 3f;"
399 1.26 matt "1: mnegl $1,%0;"
400 1.26 matt " brb 3f;"
401 1.26 matt "2: clrl %0;"
402 1.26 matt " 3:;"
403 1.19 matt : "=&g"(ret)
404 1.18 ragge : "r"(header)
405 1.18 ragge : "memory");
406 1.18 ragge
407 1.18 ragge return ret;
408 1.18 ragge }
409 1.18 ragge #define ILCK_FAILED -1 /* Interlock failed */
410 1.18 ragge #define Q_EMPTY 0 /* Queue is/was empty */
411 1.18 ragge #define Q_OK 1 /* Inserted OK */
412 1.18 ragge
413 1.31 kleink #endif /* !_VAX_MACROS_H_ && !__lint__ */
414