source: mainline/uspace/lib/c/arch/arm32/src/atomic.c@ 133461c

ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 133461c was 133461c, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 2 years ago

Align arm32 atomic op prototypes with compiler's expectations

Based on a patch by @vhotspur, but without explicit casts
between pointer types. Those are evil footguns.

  • Property mode set to 100644
File size: 5.1 KB
RevLine 
[d630139]1/*
[6b781c0]2 * Copyright (c) 2007 Michal Kebrt
[ffa73c6]3 * Copyright (c) 2018 CZ.NIC, z.s.p.o.
[d630139]4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * - 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 * - 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
[ffa73c6]30/*
31 * Older ARMs don't have atomic instructions, so we need to define a bunch
32 * of symbols for GCC to use.
[d630139]33 */
34
[3e6a98c5]35#include <stdbool.h>
[eb630cf]36#include "ras_page.h"
[8d04f709]37
[3ffb69b]38volatile unsigned *ras_page;
[8d04f709]39
[133461c]40bool __atomic_compare_exchange_4(volatile void *mem0, void *expected0,
41 unsigned desired, bool weak, int success, int failure)
[8d04f709]42{
[133461c]43 volatile unsigned *mem = mem0;
44 unsigned *expected = expected0;
45
[ffa73c6]46 (void) success;
47 (void) failure;
48 (void) weak;
49
50 unsigned ov = *expected;
51 unsigned ret;
[a35b458]52
[cd769305]53 /*
54 * The following instructions between labels 1 and 2 constitute a
[7137f74c]55 * Restartable Atomic Sequence. Should the sequence be non-atomic,
[cd769305]56 * the kernel will restart it.
57 */
58 asm volatile (
[1433ecda]59 "1:\n"
60 " adr %[ret], 1b\n"
61 " str %[ret], %[rp0]\n"
62 " adr %[ret], 2f\n"
63 " str %[ret], %[rp1]\n"
[ffa73c6]64
[1433ecda]65 " ldr %[ret], %[addr]\n"
66 " cmp %[ret], %[ov]\n"
67 " streq %[nv], %[addr]\n"
68 "2:\n"
[ffa73c6]69 : [ret] "=&r" (ret),
[1433ecda]70 [rp0] "=m" (ras_page[0]),
71 [rp1] "=m" (ras_page[1]),
[ffa73c6]72 [addr] "+m" (*mem)
[1433ecda]73 : [ov] "r" (ov),
[ffa73c6]74 [nv] "r" (desired)
[1433ecda]75 : "memory"
[cd769305]76 );
[a35b458]77
[cd769305]78 ras_page[0] = 0;
79 ras_page[1] = 0xffffffff;
[a35b458]80
[ffa73c6]81 if (ret == ov)
82 return true;
83
84 *expected = ret;
85 return false;
[8d04f709]86}
87
[133461c]88unsigned short __atomic_fetch_add_2(volatile void *mem0, unsigned short val,
89 int model)
[508b0df1]90{
[133461c]91 volatile unsigned short *mem = mem0;
92
[508b0df1]93 (void) model;
94
95 unsigned short ret;
96
97 /*
98 * The following instructions between labels 1 and 2 constitute a
99 * Restartable Atomic Seqeunce. Should the sequence be non-atomic,
100 * the kernel will restart it.
101 */
102 asm volatile (
103 "1:\n"
104 " adr %[ret], 1b\n"
105 " str %[ret], %[rp0]\n"
106 " adr %[ret], 2f\n"
107 " str %[ret], %[rp1]\n"
108 " ldrh %[ret], %[addr]\n"
109 " add %[ret], %[ret], %[imm]\n"
110 " strh %[ret], %[addr]\n"
111 "2:\n"
112 : [ret] "=&r" (ret),
113 [rp0] "=m" (ras_page[0]),
114 [rp1] "=m" (ras_page[1]),
115 [addr] "+m" (*mem)
116 : [imm] "r" (val)
117 );
118
119 ras_page[0] = 0;
120 ras_page[1] = 0xffffffff;
121
122 return ret - val;
123}
124
[133461c]125unsigned __atomic_fetch_add_4(volatile void *mem0, unsigned val, int model)
[d630139]126{
[133461c]127 volatile unsigned *mem = mem0;
128
[ffa73c6]129 (void) model;
130
131 unsigned ret;
[a35b458]132
[cd769305]133 /*
134 * The following instructions between labels 1 and 2 constitute a
135 * Restartable Atomic Seqeunce. Should the sequence be non-atomic,
136 * the kernel will restart it.
137 */
[6b781c0]138 asm volatile (
[1433ecda]139 "1:\n"
140 " adr %[ret], 1b\n"
141 " str %[ret], %[rp0]\n"
142 " adr %[ret], 2f\n"
143 " str %[ret], %[rp1]\n"
144 " ldr %[ret], %[addr]\n"
145 " add %[ret], %[ret], %[imm]\n"
146 " str %[ret], %[addr]\n"
147 "2:\n"
[ffa73c6]148 : [ret] "=&r" (ret),
[1433ecda]149 [rp0] "=m" (ras_page[0]),
150 [rp1] "=m" (ras_page[1]),
[ffa73c6]151 [addr] "+m" (*mem)
152 : [imm] "r" (val)
[6b781c0]153 );
[a35b458]154
[cd769305]155 ras_page[0] = 0;
156 ras_page[1] = 0xffffffff;
[a35b458]157
[ffa73c6]158 return ret - val;
[d630139]159}
160
[133461c]161unsigned __atomic_fetch_sub_4(volatile void *mem, unsigned val, int model)
[de7663f]162{
[ffa73c6]163 return __atomic_fetch_add_4(mem, -val, model);
[de7663f]164}
[6b781c0]165
[ffa73c6]166void __sync_synchronize(void)
[de7663f]167{
[ffa73c6]168 // FIXME: Full memory barrier. We might need a syscall for this.
[de7663f]169}
[d630139]170
[ffa73c6]171unsigned __sync_add_and_fetch_4(volatile void *vptr, unsigned val)
[de7663f]172{
[ffa73c6]173 return __atomic_fetch_add_4(vptr, val, __ATOMIC_SEQ_CST) + val;
[de7663f]174}
[6b781c0]175
[ffa73c6]176unsigned __sync_sub_and_fetch_4(volatile void *vptr, unsigned val)
[de7663f]177{
[ffa73c6]178 return __atomic_fetch_sub_4(vptr, val, __ATOMIC_SEQ_CST) - val;
[de7663f]179}
[6b781c0]180
[ffa73c6]181bool __sync_bool_compare_and_swap_4(volatile void *ptr, unsigned old_val, unsigned new_val)
[de7663f]182{
[ffa73c6]183 return __atomic_compare_exchange_4(ptr, &old_val, new_val, false,
184 __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST);
[de7663f]185}
[6b781c0]186
[ffa73c6]187unsigned __sync_val_compare_and_swap_4(volatile void *ptr, unsigned old_val, unsigned new_val)
[de7663f]188{
[ffa73c6]189 __atomic_compare_exchange_4(ptr, &old_val, new_val, false,
190 __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST);
191 return old_val;
[de7663f]192}
Note: See TracBrowser for help on using the repository browser.