1/*
2 * Copyright (c) 2009 Corey Tabaka
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files
6 * (the "Software"), to deal in the Software without restriction,
7 * including without limitation the rights to use, copy, modify, merge,
8 * publish, distribute, sublicense, and/or sell copies of the Software,
9 * and to permit persons to whom the Software is furnished to do so,
10 * subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
23#include <asm.h>
24
25.text
26
27/* This follows the x86-64 ABI, the parameters are stored in registers in the following order*/
28/*
29%rdi used to pass 1st argument
30%rsi used to pass 2nd argument
31%rdx used to pass 3rd argument and 2nd return register
32%rcx used to pass 4th argument
33%r8 used to pass 5th argument
34%r9 used to pass 6th argument
35%rax 1st return register
36*/
37
38/* int _atomic_and(int *ptr, int val); */
39FUNCTION(_atomic_and)
40    mov (%rdi), %eax
410:
42    mov %eax, %ecx
43    and %esi, %ecx
44    lock
45    cmpxchg %ecx, (%rdi)
46    jnz 1f                  /* static prediction: branch forward not taken */
47    ret
481:
49    jmp 0b
50
51
52/* int _atomic_or(int *ptr, int val); */
53FUNCTION(_atomic_or)
54
55    mov (%rdi), %eax
560:
57    mov %eax, %ecx
58    or  %esi, %ecx
59    lock
60    cmpxchg %ecx, (%rdi)
61    jnz 1f                  /* static prediction: branch forward not taken */
62    ret
631:
64    jmp 0b
65
66/* void arch_idle(); */
67FUNCTION(arch_idle)
68    pushf
69    popq %rax
70    andq $0x200, %rax
71    test %rax, %rax
72    je 1f                   /* don't halt if local interrupts are disabled */
73    hlt
741:
75    ret
76
77