blob: bf2c6074efd2fb97886d974dea2668f8da59c49c [file] [log] [blame]
Jan Beulicha73866942011-07-19 13:00:19 +01001/*
2 * x86 semaphore implementation.
3 *
4 * (C) Copyright 1999 Linus Torvalds
5 *
6 * Portions Copyright 1999 Red Hat, Inc.
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version
11 * 2 of the License, or (at your option) any later version.
12 *
13 * rw semaphores implemented November 1999 by Benjamin LaHaise <bcrl@kvack.org>
14 */
15
16#include <linux/linkage.h>
17#include <asm/alternative-asm.h>
Josh Poimboeuf3387a532016-01-21 16:49:22 -060018#include <asm/frame.h>
Jan Beulicha73866942011-07-19 13:00:19 +010019
20#define __ASM_HALF_REG(reg) __ASM_SEL(reg, e##reg)
21#define __ASM_HALF_SIZE(inst) __ASM_SEL(inst##w, inst##l)
22
23#ifdef CONFIG_X86_32
24
25/*
26 * The semaphore operations have a special calling sequence that
27 * allow us to do a simpler in-line version of them. These routines
28 * need to convert that sequence back into the C sequence when
29 * there is contention on the semaphore.
30 *
31 * %eax contains the semaphore pointer on entry. Save the C-clobbered
Borislav Petkov4544ba82016-05-16 11:34:28 +020032 * registers (%eax, %edx and %ecx) except %eax which is either a return
33 * value or just gets clobbered. Same is true for %edx so make sure GCC
34 * reloads it after the slow path, by making it hold a temporary, for
35 * example see ____down_write().
Jan Beulicha73866942011-07-19 13:00:19 +010036 */
37
38#define save_common_regs \
Ingo Molnar131484c2015-05-28 12:21:47 +020039 pushl %ecx
Jan Beulicha73866942011-07-19 13:00:19 +010040
41#define restore_common_regs \
Ingo Molnar131484c2015-05-28 12:21:47 +020042 popl %ecx
Jan Beulicha73866942011-07-19 13:00:19 +010043
44 /* Avoid uglifying the argument copying x86-64 needs to do. */
45 .macro movq src, dst
46 .endm
47
48#else
49
50/*
51 * x86-64 rwsem wrappers
52 *
53 * This interfaces the inline asm code to the slow-path
54 * C routines. We need to save the call-clobbered regs
55 * that the asm does not mark as clobbered, and move the
56 * argument from %rax to %rdi.
57 *
58 * NOTE! We don't need to save %rax, because the functions
59 * will always return the semaphore pointer in %rax (which
60 * is also the input argument to these helpers)
61 *
62 * The following can clobber %rdx because the asm clobbers it:
63 * call_rwsem_down_write_failed
64 * call_rwsem_wake
65 * but %rdi, %rsi, %rcx, %r8-r11 always need saving.
66 */
67
68#define save_common_regs \
Ingo Molnar131484c2015-05-28 12:21:47 +020069 pushq %rdi; \
70 pushq %rsi; \
71 pushq %rcx; \
72 pushq %r8; \
73 pushq %r9; \
74 pushq %r10; \
75 pushq %r11
Jan Beulicha73866942011-07-19 13:00:19 +010076
77#define restore_common_regs \
Ingo Molnar131484c2015-05-28 12:21:47 +020078 popq %r11; \
79 popq %r10; \
80 popq %r9; \
81 popq %r8; \
82 popq %rcx; \
83 popq %rsi; \
84 popq %rdi
Jan Beulicha73866942011-07-19 13:00:19 +010085
86#endif
87
88/* Fix up special calling conventions */
89ENTRY(call_rwsem_down_read_failed)
Josh Poimboeuf3387a532016-01-21 16:49:22 -060090 FRAME_BEGIN
Jan Beulicha73866942011-07-19 13:00:19 +010091 save_common_regs
Ingo Molnar131484c2015-05-28 12:21:47 +020092 __ASM_SIZE(push,) %__ASM_REG(dx)
Jan Beulicha73866942011-07-19 13:00:19 +010093 movq %rax,%rdi
94 call rwsem_down_read_failed
Ingo Molnar131484c2015-05-28 12:21:47 +020095 __ASM_SIZE(pop,) %__ASM_REG(dx)
Jan Beulicha73866942011-07-19 13:00:19 +010096 restore_common_regs
Josh Poimboeuf3387a532016-01-21 16:49:22 -060097 FRAME_END
Jan Beulicha73866942011-07-19 13:00:19 +010098 ret
Jan Beulicha73866942011-07-19 13:00:19 +010099ENDPROC(call_rwsem_down_read_failed)
100
101ENTRY(call_rwsem_down_write_failed)
Josh Poimboeuf3387a532016-01-21 16:49:22 -0600102 FRAME_BEGIN
Jan Beulicha73866942011-07-19 13:00:19 +0100103 save_common_regs
104 movq %rax,%rdi
105 call rwsem_down_write_failed
106 restore_common_regs
Josh Poimboeuf3387a532016-01-21 16:49:22 -0600107 FRAME_END
Jan Beulicha73866942011-07-19 13:00:19 +0100108 ret
Jan Beulicha73866942011-07-19 13:00:19 +0100109ENDPROC(call_rwsem_down_write_failed)
110
Michal Hocko664b4e22016-04-07 17:12:30 +0200111ENTRY(call_rwsem_down_write_failed_killable)
Michal Hocko00fb16e2016-04-13 11:57:12 +0200112 FRAME_BEGIN
Michal Hocko664b4e22016-04-07 17:12:30 +0200113 save_common_regs
114 movq %rax,%rdi
115 call rwsem_down_write_failed_killable
116 restore_common_regs
Michal Hocko00fb16e2016-04-13 11:57:12 +0200117 FRAME_END
Michal Hocko664b4e22016-04-07 17:12:30 +0200118 ret
119ENDPROC(call_rwsem_down_write_failed_killable)
120
Jan Beulicha73866942011-07-19 13:00:19 +0100121ENTRY(call_rwsem_wake)
Josh Poimboeuf3387a532016-01-21 16:49:22 -0600122 FRAME_BEGIN
Jan Beulicha73866942011-07-19 13:00:19 +0100123 /* do nothing if still outstanding active readers */
124 __ASM_HALF_SIZE(dec) %__ASM_HALF_REG(dx)
125 jnz 1f
126 save_common_regs
127 movq %rax,%rdi
128 call rwsem_wake
129 restore_common_regs
Josh Poimboeuf3387a532016-01-21 16:49:22 -06001301: FRAME_END
131 ret
Jan Beulicha73866942011-07-19 13:00:19 +0100132ENDPROC(call_rwsem_wake)
133
134ENTRY(call_rwsem_downgrade_wake)
Josh Poimboeuf3387a532016-01-21 16:49:22 -0600135 FRAME_BEGIN
Jan Beulicha73866942011-07-19 13:00:19 +0100136 save_common_regs
Ingo Molnar131484c2015-05-28 12:21:47 +0200137 __ASM_SIZE(push,) %__ASM_REG(dx)
Jan Beulicha73866942011-07-19 13:00:19 +0100138 movq %rax,%rdi
139 call rwsem_downgrade_wake
Ingo Molnar131484c2015-05-28 12:21:47 +0200140 __ASM_SIZE(pop,) %__ASM_REG(dx)
Jan Beulicha73866942011-07-19 13:00:19 +0100141 restore_common_regs
Josh Poimboeuf3387a532016-01-21 16:49:22 -0600142 FRAME_END
Jan Beulicha73866942011-07-19 13:00:19 +0100143 ret
Jan Beulicha73866942011-07-19 13:00:19 +0100144ENDPROC(call_rwsem_downgrade_wake)