xref: /netbsd-src/sys/arch/sh3/sh3/lock_stubs.S (revision 6740bb544022b529a034d40e8f1cdba1f0438538)
1/*	$NetBSD: lock_stubs.S,v 1.13 2008/05/25 15:56:12 chs Exp $	*/
2
3/*
4 * Copyright (c) 2007 Valeriy E. Ushakov
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. The name of the author may not be used to endorse or promote products
16 *    derived from this software without specific prior written permission
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30#include <sh3/asm.h>
31#include <sh3/psl.h>
32
33#include "opt_lockdebug.h"
34
35
36/*
37 * LINTSTUB: include <sys/types.h>
38 * LINTSTUB: include <sys/mutex.h>
39 */
40
41
42/*
43 * LINTSTUB: Func: uintptr_t _lock_cas(volatile uintptr_t *ptr, uintptr_t old, uintptr_t new);
44 *
45 *	Atomic compare-and-swap for kernel use.  SuperH machines are
46 *	uniprocessor, so we need to be atomic only w.r.t. interrupts.
47 *	Implement this as (the only in-kernel) RAS, with restart check
48 *	done on return from interrupt.
49 *
50 *	Mutex stubs below depend on this function:
51 *	    r4 - preserved, passed along to mutex_vector_{enter,exit}
52 *	    r7 - preserved, used to save pr (to avoid touching stack)
53 *	     T - still reflects cas success status (to avoid another test)
54 */
55ENTRY(_lock_cas)
56ALTENTRY(_lock_cas_ras_start)
57	mov.l	@r4, r0
58	cmp/eq	r0, r5		! T = (*ptr == old)
59	bf	1f
60	mov.l	r6, @r4		! *ptr = new
61ALTENTRY(_lock_cas_ras_end)
621:	rts
63	 nop			! retval = *ptr at time of compare
64	SET_ENTRY_SIZE(_lock_cas)
65
66STRONG_ALIAS(_atomic_cas_ulong,_lock_cas)
67STRONG_ALIAS(atomic_cas_ulong,_lock_cas)
68STRONG_ALIAS(_atomic_cas_32,_lock_cas)
69STRONG_ALIAS(atomic_cas_32,_lock_cas)
70STRONG_ALIAS(_atomic_cas_uint,_lock_cas)
71STRONG_ALIAS(atomic_cas_uint,_lock_cas)
72STRONG_ALIAS(_atomic_cas_ptr,_lock_cas)
73STRONG_ALIAS(atomic_cas_ptr,_lock_cas)
74
75STRONG_ALIAS(_atomic_cas_ulong_ni,_lock_cas)
76STRONG_ALIAS(atomic_cas_ulong_ni,_lock_cas)
77STRONG_ALIAS(_atomic_cas_32_ni,_lock_cas)
78STRONG_ALIAS(atomic_cas_32_ni,_lock_cas)
79STRONG_ALIAS(_atomic_cas_uint_ni,_lock_cas)
80STRONG_ALIAS(atomic_cas_uint_ni,_lock_cas)
81STRONG_ALIAS(_atomic_cas_ptr_ni,_lock_cas)
82STRONG_ALIAS(atomic_cas_ptr_ni,_lock_cas)
83
84#if !defined(LOCKDEBUG)
85
86/*
87 * LINTSTUB: Func: void mutex_enter(kmutex_t *mtx);
88 */
89ENTRY(mutex_enter)
90	sts	pr, r7		! depend on _lock_cas not clobbering r7
91	mov.l	.L_curlwp, r6
92	mov	#0, r5
93	bsr	_lock_cas	! _lock_cas(&mtx->mtx_owner, 0, curlwp)
94	 mov.l	@r6, r6
95	!! T bit still indicates if cas was successful
96	bf.s	1f		! hard case if cas failed
97	 lds	r7, pr
98	rts
99	 nop
100
101	!! depend on _lock_cas not clobbering mtx in r4
1021:	mov.l	.L_mutex_vector_enter, r0
103	jmp	@r0
104	 nop
105	/* NOTREACHED */
106
107
108/*
109 * LINTSTUB: Func: void mutex_exit(kmutex_t *mtx);
110 */
111ENTRY(mutex_exit)
112	sts	pr, r7		! depend on _lock_cas not clobbering r7
113	mov.l	.L_curlwp, r5
114	mov	#0, r6
115	bsr	_lock_cas	! _lock_cas(&mtx->mtx_owner, curlwp, 0)
116	 mov.l	@r5, r5
117	!! T bit still indicates if cas was successful
118	bf.s	1f		! hard case if cas failed
119	 lds	r7, pr
120	rts
121	 nop
122
123	!! depend on _lock_cas not clobbering mtx in r4
1241:	mov.l	.L_mutex_vector_exit, r0
125	jmp	@r0
126	 nop
127	/* NOTREACHED */
128
129	.align 2
130.L_curlwp:		.long	_C_LABEL(curlwp)
131.L_mutex_vector_enter:	.long	_C_LABEL(mutex_vector_enter)
132.L_mutex_vector_exit:	.long	_C_LABEL(mutex_vector_exit)
133
134#endif /* !LOCKDEBUG */
135