Home | History | Annotate | Line # | Download | only in include
macros.h revision 1.41
      1  1.41      yamt /*	$NetBSD: macros.h,v 1.41 2007/05/17 14:51:33 yamt 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.40      matt #if !__GNUC_PREREQ__(4, 1)
     42   1.1     ragge /* Here general macros are supposed to be stored */
     43   1.1     ragge 
     44  1.36     perry static __inline int __attribute__((__unused__))
     45  1.37      matt vax_ffs(int reg)
     46  1.16     ragge {
     47   1.3     ragge 	register int val;
     48   1.3     ragge 
     49  1.35     perry 	__asm volatile ("ffs $0,$32,%1,%0;"
     50  1.25      matt 			    "bneq 1f;"
     51  1.25      matt 			    "mnegl $1,%0;"
     52  1.25      matt 			    "1:;"
     53  1.25      matt 			    "incl %0"
     54  1.19      matt 			: "=&r" (val)
     55   1.3     ragge 			: "r" (reg) );
     56   1.3     ragge 	return	val;
     57   1.3     ragge }
     58  1.37      matt #define ffs vax_ffs
     59  1.40      matt #endif
     60   1.3     ragge 
     61  1.36     perry static __inline void __attribute__((__unused__))
     62  1.37      matt vax_remque(void *p)
     63  1.16     ragge {
     64  1.35     perry 	__asm volatile ("remque (%0),%0;clrl 4(%0)"
     65   1.3     ragge 			:
     66   1.3     ragge 			: "r" (p)
     67   1.3     ragge 			: "memory" );
     68   1.3     ragge }
     69  1.37      matt #define _remque vax_remque
     70   1.3     ragge 
     71  1.36     perry static __inline void  __attribute__((__unused__))
     72  1.37      matt vax_insque(void *p, void *q)
     73  1.16     ragge {
     74  1.35     perry 	__asm volatile ("insque (%0),(%1)"
     75  1.15     ragge 			:
     76  1.15     ragge 			: "r" (p),"r" (q)
     77  1.15     ragge 			: "memory" );
     78   1.3     ragge }
     79  1.37      matt #define _insque vax_insque
     80   1.3     ragge 
     81  1.37      matt #if 0
     82  1.38  christos static __inline void *__attribute__((__unused__))
     83  1.37      matt vax_memcpy(void *to, const void *from, size_t len)
     84  1.16     ragge {
     85  1.29     ragge 	if (len > 65535) {
     86  1.29     ragge 		__blkcpy(from, to, len);
     87  1.29     ragge 	} else {
     88  1.37      matt 		__asm volatile ("movc3 %1,(%2),%0"
     89  1.37      matt 			: "=m" (*(char *)to)
     90  1.37      matt 			: "g" (len), "r" (*(const char *)from)
     91  1.16     ragge 			:"r0","r1","r2","r3","r4","r5","memory","cc");
     92  1.29     ragge 	}
     93  1.22      matt 	return to;
     94  1.16     ragge }
     95  1.37      matt #define memcpy vax_memcpy
     96  1.37      matt 
     97  1.38  christos static __inline void *__attribute__((__unused__))
     98  1.37      matt vax_memmove(void *to, const void *from, size_t len)
     99  1.16     ragge {
    100  1.29     ragge 	if (len > 65535) {
    101  1.29     ragge 		__blkcpy(from, to, len);
    102  1.29     ragge 	} else {
    103  1.39      matt 		__asm __volatile ("movc3 %1,%2,%0"
    104  1.37      matt 			: "=m" (*(char *)to)
    105  1.37      matt 			: "g" (len), "mo" (*(const char *)from)
    106  1.16     ragge 			:"r0","r1","r2","r3","r4","r5","memory","cc");
    107  1.29     ragge 	}
    108  1.22      matt 	return to;
    109  1.16     ragge }
    110  1.37      matt #define memmove vax_memmove
    111  1.29     ragge #endif
    112  1.15     ragge 
    113  1.38  christos static __inline void *__attribute__((__unused__))
    114  1.37      matt vax_memset(void *block, int c, size_t len)
    115  1.16     ragge {
    116  1.29     ragge 	if (len > 65535) {
    117  1.24   thorpej 		__blkset(block, c, len);
    118  1.29     ragge 	} else {
    119  1.39      matt 		__asm __volatile ("movc5 $0,(%%sp),%2,%1,%0"
    120  1.37      matt 			: "=m" (*(char *)block)
    121  1.37      matt 			:  "g" (len), "g" (c)
    122  1.16     ragge 			:"r0","r1","r2","r3","r4","r5","memory","cc");
    123  1.16     ragge 	}
    124  1.16     ragge 	return block;
    125  1.16     ragge }
    126  1.37      matt #define memset vax_memset
    127   1.3     ragge 
    128  1.29     ragge #ifdef notdef
    129  1.16     ragge /* XXX - the return syntax of memcmp is wrong */
    130  1.36     perry static __inline int __attribute__((__unused__))
    131  1.16     ragge memcmp(const void *b1, const void *b2, size_t len)
    132  1.16     ragge {
    133  1.17     ragge 	register int ret;
    134  1.16     ragge 
    135  1.35     perry 	__asm volatile("cmpc3 %3,(%1),(%2);"
    136  1.25      matt 			   "movl %%r0,%0"
    137  1.16     ragge 			: "=r" (ret)
    138  1.16     ragge 			: "r" (b1), "r" (b2), "r" (len)
    139  1.16     ragge 			: "r0","r1","r2","r3" );
    140  1.16     ragge 	return ret;
    141  1.16     ragge }
    142  1.16     ragge 
    143  1.36     perry static __inline int __attribute__((__unused__))
    144  1.16     ragge bcmp(const void *b1, const void *b2, size_t len)
    145  1.16     ragge {
    146  1.17     ragge 	register int ret;
    147   1.3     ragge 
    148  1.35     perry 	__asm volatile("cmpc3 %3,(%1),(%2);"
    149  1.25      matt 			   "movl %%r0,%0"
    150   1.3     ragge 			: "=r" (ret)
    151   1.3     ragge 			: "r" (b1), "r" (b2), "r" (len)
    152   1.3     ragge 			: "r0","r1","r2","r3" );
    153   1.3     ragge 	return ret;
    154   1.3     ragge }
    155   1.3     ragge 
    156  1.16     ragge /* Begin nya */
    157  1.36     perry static __inline size_t __attribute__((__unused__))
    158  1.16     ragge strlen(const char *cp)
    159  1.16     ragge {
    160  1.17     ragge         register size_t ret;
    161  1.16     ragge 
    162  1.35     perry         __asm volatile("locc $0,$65535,(%1);"
    163  1.25      matt 			   "subl3 %%r0,$65535,%0"
    164  1.16     ragge                         : "=r" (ret)
    165  1.16     ragge                         : "r" (cp)
    166  1.16     ragge                         : "r0","r1","cc" );
    167  1.16     ragge         return  ret;
    168  1.16     ragge }
    169  1.16     ragge 
    170  1.36     perry static __inline char * __attribute__((__unused__))
    171  1.16     ragge strcat(char *cp, const char *c2)
    172  1.16     ragge {
    173  1.35     perry         __asm volatile("locc $0,$65535,(%1);"
    174  1.25      matt 			   "subl3 %%r0,$65535,%%r2;"
    175  1.25      matt 			   "incl %%r2;"
    176  1.25      matt                            "locc $0,$65535,(%0);"
    177  1.25      matt 			   "movc3 %%r2,(%1),(%%r1)"
    178  1.16     ragge                         :
    179  1.16     ragge                         : "r" (cp), "r" (c2)
    180  1.16     ragge                         : "r0","r1","r2","r3","r4","r5","memory","cc");
    181  1.16     ragge         return  cp;
    182  1.16     ragge }
    183  1.16     ragge 
    184  1.36     perry static __inline char * __attribute__((__unused__))
    185  1.16     ragge strncat(char *cp, const char *c2, size_t count)
    186  1.16     ragge {
    187  1.35     perry         __asm volatile("locc $0,%2,(%1);"
    188  1.28   thorpej 			   "subl3 %%r0,%2,%%r2;"
    189  1.25      matt                            "locc $0,$65535,(%0);"
    190  1.25      matt 			   "movc3 %%r2,(%1),(%%r1);"
    191  1.25      matt 			   "movb $0,(%%r3)"
    192  1.16     ragge                         :
    193  1.16     ragge                         : "r" (cp), "r" (c2), "g"(count)
    194  1.16     ragge                         : "r0","r1","r2","r3","r4","r5","memory","cc");
    195  1.16     ragge         return  cp;
    196  1.16     ragge }
    197  1.16     ragge 
    198  1.36     perry static __inline char * __attribute__((__unused__))
    199  1.16     ragge strcpy(char *cp, const char *c2)
    200  1.16     ragge {
    201  1.35     perry         __asm volatile("locc $0,$65535,(%1);"
    202  1.25      matt 			   "subl3 %%r0,$65535,%%r2;"
    203  1.25      matt                            "movc3 %%r2,(%1),(%0);"
    204  1.25      matt 			   "movb $0,(%%r3)"
    205  1.16     ragge                         :
    206  1.16     ragge                         : "r" (cp), "r" (c2)
    207  1.16     ragge                         : "r0","r1","r2","r3","r4","r5","memory","cc");
    208  1.16     ragge         return  cp;
    209  1.16     ragge }
    210  1.16     ragge 
    211  1.36     perry static __inline char * __attribute__((__unused__))
    212  1.16     ragge strncpy(char *cp, const char *c2, size_t len)
    213  1.16     ragge {
    214  1.35     perry         __asm volatile("movl %2,%%r2;"
    215  1.25      matt 			   "locc $0,%%r2,(%1);"
    216  1.25      matt 			   "beql 1f;"
    217  1.25      matt 			   "subl3 %%r0,%2,%%r2;"
    218  1.25      matt                            "clrb (%0)[%%r2];"
    219  1.25      matt 			   "1:;"
    220  1.25      matt 			   "movc3 %%r2,(%1),(%0)"
    221  1.16     ragge                         :
    222  1.16     ragge                         : "r" (cp), "r" (c2), "g"(len)
    223  1.16     ragge                         : "r0","r1","r2","r3","r4","r5","memory","cc");
    224  1.16     ragge         return  cp;
    225  1.16     ragge }
    226  1.16     ragge 
    227  1.38  christos static __inline void *__attribute__((__unused__))
    228  1.16     ragge memchr(const void *cp, int c, size_t len)
    229  1.16     ragge {
    230  1.16     ragge         void *ret;
    231  1.35     perry         __asm volatile("locc %2,%3,(%1);"
    232  1.25      matt 			   "bneq 1f;"
    233  1.25      matt 			   "clrl %%r1;"
    234  1.25      matt 			   "1:;"
    235  1.25      matt 			   "movl %%r1,%0"
    236  1.16     ragge                         : "=g"(ret)
    237  1.16     ragge                         : "r" (cp), "r" (c), "g"(len)
    238  1.16     ragge                         : "r0","r1","cc");
    239  1.16     ragge         return  ret;
    240  1.16     ragge }
    241  1.16     ragge 
    242  1.36     perry static __inline int __attribute__((__unused__))
    243  1.16     ragge strcmp(const char *cp, const char *c2)
    244  1.16     ragge {
    245  1.17     ragge         register int ret;
    246  1.35     perry         __asm volatile("locc $0,$65535,(%1);"
    247  1.25      matt 			   "subl3 %%r0,$65535,%%r0;"
    248  1.25      matt 			   "incl %%r0;"
    249  1.25      matt                            "cmpc3 %%r0,(%1),(%2);"
    250  1.25      matt 			   "beql 1f;"
    251  1.25      matt 			   "movl $1,%%r2;"
    252  1.25      matt                            "cmpb (%%r1),(%%r3);"
    253  1.25      matt 			   "bcc 1f;"
    254  1.25      matt 			   "mnegl $1,%%r2;"
    255  1.25      matt 			   "1:;"
    256  1.25      matt 			   "movl %%r2,%0"
    257  1.16     ragge                         : "=g"(ret)
    258  1.16     ragge                         : "r" (cp), "r" (c2)
    259  1.16     ragge                         : "r0","r1","r2","r3","cc");
    260  1.16     ragge         return  ret;
    261  1.16     ragge }
    262  1.29     ragge #endif
    263  1.16     ragge 
    264   1.9       cgd #if 0 /* unused, but no point in deleting it since it _is_ an instruction */
    265  1.36     perry static __inline int __attribute__((__unused__))
    266  1.24   thorpej locc(int mask, char *cp, size_t size){
    267   1.3     ragge 	register ret;
    268   1.3     ragge 
    269  1.35     perry 	__asm volatile("locc %1,%2,(%3);"
    270  1.25      matt 			   "movl %%r0,%0"
    271   1.3     ragge 			: "=r" (ret)
    272   1.3     ragge 			: "r" (mask),"r"(size),"r"(cp)
    273   1.3     ragge 			: "r0","r1" );
    274   1.3     ragge 	return	ret;
    275   1.3     ragge }
    276   1.9       cgd #endif
    277   1.3     ragge 
    278  1.36     perry static __inline int __attribute__((__unused__))
    279  1.37      matt vax_scanc(u_int size, const u_char *cp, const u_char *table, int mask)
    280  1.16     ragge {
    281  1.17     ragge 	register int ret;
    282   1.3     ragge 
    283  1.35     perry 	__asm volatile("scanc %1,(%2),(%3),%4;"
    284  1.25      matt 			   "movl %%r0,%0"
    285   1.3     ragge 			: "=g"(ret)
    286   1.3     ragge 			: "r"(size),"r"(cp),"r"(table),"r"(mask)
    287   1.3     ragge 			: "r0","r1","r2","r3" );
    288   1.3     ragge 	return ret;
    289   1.3     ragge }
    290  1.37      matt #define scanc vax_scanc
    291   1.3     ragge 
    292  1.36     perry static __inline int __attribute__((__unused__))
    293  1.37      matt vax_skpc(int mask, size_t size, u_char *cp)
    294  1.16     ragge {
    295  1.17     ragge 	register int ret;
    296   1.3     ragge 
    297  1.35     perry 	__asm volatile("skpc %1,%2,(%3);"
    298  1.25      matt 			   "movl %%r0,%0"
    299   1.3     ragge 			: "=g"(ret)
    300   1.3     ragge 			: "r"(mask),"r"(size),"r"(cp)
    301   1.3     ragge 			: "r0","r1" );
    302   1.3     ragge 	return	ret;
    303  1.23     ragge }
    304  1.37      matt #define skpc vax_skpc
    305  1.23     ragge 
    306  1.23     ragge /*
    307  1.23     ragge  * Set/clear a bit at a memory position; interlocked.
    308  1.23     ragge  * Return 0 if already set, 1 otherwise.
    309  1.23     ragge  */
    310  1.36     perry static __inline int __attribute__((__unused__))
    311  1.23     ragge bbssi(int bitnr, long *addr)
    312  1.23     ragge {
    313  1.23     ragge 	register int ret;
    314  1.23     ragge 
    315  1.35     perry 	__asm volatile("clrl %%r0;"
    316  1.25      matt 			   "bbssi %1,%2,1f;"
    317  1.25      matt 			   "incl %%r0;"
    318  1.25      matt 			   "1:;"
    319  1.25      matt 			   "movl %%r0,%0"
    320  1.23     ragge 		: "=&r"(ret)
    321  1.23     ragge 		: "g"(bitnr),"m"(*addr)
    322  1.23     ragge 		: "r0","cc","memory");
    323  1.23     ragge 	return ret;
    324  1.23     ragge }
    325  1.23     ragge 
    326  1.36     perry static __inline int __attribute__((__unused__))
    327  1.23     ragge bbcci(int bitnr, long *addr)
    328  1.23     ragge {
    329  1.23     ragge 	register int ret;
    330  1.23     ragge 
    331  1.35     perry 	__asm volatile("clrl %%r0;"
    332  1.25      matt 			   "bbcci %1,%2,1f;"
    333  1.25      matt 			   "incl %%r0;"
    334  1.25      matt 			   "1:;"
    335  1.25      matt 			   "movl %%r0,%0"
    336  1.23     ragge 		: "=&r"(ret)
    337  1.23     ragge 		: "g"(bitnr),"m"(*addr)
    338  1.23     ragge 		: "r0","cc","memory");
    339  1.23     ragge 	return ret;
    340   1.3     ragge }
    341   1.3     ragge 
    342  1.41      yamt static inline struct lwp *
    343  1.41      yamt cpu_switchto(struct lwp *oldlwp, struct lwp *newlwp)
    344  1.41      yamt {
    345  1.41      yamt 	struct lwp *prevlwp;
    346  1.41      yamt 	__asm volatile(
    347  1.41      yamt 		"movl %1,%%r0;"
    348  1.41      yamt 		"movl %2,%%r1;"
    349  1.41      yamt 		"movpsl -(%%sp);"
    350  1.41      yamt 		"jsb Swtchto;"
    351  1.41      yamt 		"movl %%r0,%0"
    352  1.41      yamt 	    : "=g"(prevlwp)
    353  1.41      yamt 	    : "g" (oldlwp), "g" (newlwp)
    354  1.41      yamt 	    : "r0", "r1");
    355  1.41      yamt 	return prevlwp;
    356  1.41      yamt }
    357  1.18     ragge 
    358  1.18     ragge /*
    359  1.18     ragge  * Interlock instructions. Used both in multiprocessor environments to
    360  1.18     ragge  * lock between CPUs and in uniprocessor systems when locking is required
    361  1.18     ragge  * between I/O devices and the master CPU.
    362  1.18     ragge  */
    363  1.18     ragge /*
    364  1.18     ragge  * Insqti() locks and inserts an element into the end of a queue.
    365  1.18     ragge  * Returns -1 if interlock failed, 1 if inserted OK and 0 if first in queue.
    366  1.18     ragge  */
    367  1.36     perry static __inline int __attribute__((__unused__))
    368  1.18     ragge insqti(void *entry, void *header) {
    369  1.18     ragge 	register int ret;
    370  1.18     ragge 
    371  1.35     perry 	__asm volatile(
    372  1.26      matt 		"	mnegl $1,%0;"
    373  1.26      matt 		"	insqti (%1),(%2);"
    374  1.26      matt 		"	bcs 1f;"		/* failed insert */
    375  1.26      matt 		"	beql 2f;"		/* jump if first entry */
    376  1.26      matt 		"	movl $1,%0;"
    377  1.26      matt 		"	brb 1f;"
    378  1.26      matt 		"2:	clrl %0;"
    379  1.26      matt 		"	1:;"
    380  1.19      matt 			: "=&g"(ret)
    381  1.18     ragge 			: "r"(entry), "r"(header)
    382  1.18     ragge 			: "memory");
    383  1.18     ragge 
    384  1.18     ragge 	return ret;
    385  1.18     ragge }
    386  1.18     ragge 
    387  1.18     ragge /*
    388  1.18     ragge  * Remqhi() removes an element from the head of the queue.
    389  1.18     ragge  * Returns -1 if interlock failed, 0 if queue empty, address of the
    390  1.18     ragge  * removed element otherwise.
    391  1.18     ragge  */
    392  1.38  christos static __inline void *__attribute__((__unused__))
    393  1.18     ragge remqhi(void *header) {
    394  1.18     ragge 	register void *ret;
    395  1.18     ragge 
    396  1.35     perry 	__asm volatile(
    397  1.26      matt 		"	remqhi (%1),%0;"
    398  1.26      matt 		"	bcs 1f;"		/* failed interlock */
    399  1.26      matt 		"	bvs 2f;"		/* nothing was removed */
    400  1.26      matt 		"	brb 3f;"
    401  1.26      matt 		"1:	mnegl $1,%0;"
    402  1.26      matt 		"	brb 3f;"
    403  1.26      matt 		"2:	clrl %0;"
    404  1.26      matt 		"	3:;"
    405  1.19      matt 			: "=&g"(ret)
    406  1.18     ragge 			: "r"(header)
    407  1.18     ragge 			: "memory");
    408  1.18     ragge 
    409  1.18     ragge 	return ret;
    410  1.18     ragge }
    411  1.18     ragge #define	ILCK_FAILED	-1	/* Interlock failed */
    412  1.18     ragge #define	Q_EMPTY		0	/* Queue is/was empty */
    413  1.18     ragge #define	Q_OK		1	/* Inserted OK */
    414  1.18     ragge 
    415  1.31    kleink #endif	/* !_VAX_MACROS_H_ && !__lint__ */
    416