]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
fa6ac760 LP |
2 | /*** |
3 | This file is part of systemd. | |
4 | ||
5 | Copyright 2014 Lennart Poettering | |
6 | ||
7 | systemd is free software; you can redistribute it and/or modify it | |
8 | under the terms of the GNU Lesser General Public License as published by | |
9 | the Free Software Foundation; either version 2.1 of the License, or | |
10 | (at your option) any later version. | |
11 | ||
12 | systemd is distributed in the hope that it will be useful, but | |
13 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 | Lesser General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU Lesser General Public License | |
18 | along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
19 | ***/ | |
20 | ||
11c3a366 | 21 | #include <errno.h> |
fa6ac760 | 22 | #include <signal.h> |
11c3a366 | 23 | #include <stddef.h> |
fa6ac760 LP |
24 | #include <sys/mman.h> |
25 | ||
26 | #include "macro.h" | |
fa6ac760 | 27 | #include "sigbus.h" |
cf0fbc49 | 28 | #include "util.h" |
fa6ac760 LP |
29 | |
30 | #define SIGBUS_QUEUE_MAX 64 | |
31 | ||
32 | static struct sigaction old_sigaction; | |
33 | static unsigned n_installed = 0; | |
34 | ||
35 | /* We maintain a fixed size list of page addresses that triggered a | |
36 | SIGBUS. We access with list with atomic operations, so that we | |
37 | don't have to deal with locks between signal handler and main | |
38 | programs in possibly multiple threads. */ | |
39 | ||
40 | static void* volatile sigbus_queue[SIGBUS_QUEUE_MAX]; | |
41 | static volatile sig_atomic_t n_sigbus_queue = 0; | |
42 | ||
43 | static void sigbus_push(void *addr) { | |
44 | unsigned u; | |
45 | ||
46 | assert(addr); | |
47 | ||
48 | /* Find a free place, increase the number of entries and leave, if we can */ | |
49 | for (u = 0; u < SIGBUS_QUEUE_MAX; u++) | |
50 | if (__sync_bool_compare_and_swap(&sigbus_queue[u], NULL, addr)) { | |
51 | __sync_fetch_and_add(&n_sigbus_queue, 1); | |
52 | return; | |
53 | } | |
54 | ||
55 | /* If we can't, make sure the queue size is out of bounds, to | |
56 | * mark it as overflow */ | |
57 | for (;;) { | |
58 | unsigned c; | |
59 | ||
60 | __sync_synchronize(); | |
61 | c = n_sigbus_queue; | |
62 | ||
63 | if (c > SIGBUS_QUEUE_MAX) /* already overflow */ | |
64 | return; | |
65 | ||
66 | if (__sync_bool_compare_and_swap(&n_sigbus_queue, c, c + SIGBUS_QUEUE_MAX)) | |
67 | return; | |
68 | } | |
69 | } | |
70 | ||
71 | int sigbus_pop(void **ret) { | |
72 | assert(ret); | |
73 | ||
74 | for (;;) { | |
75 | unsigned u, c; | |
76 | ||
77 | __sync_synchronize(); | |
78 | c = n_sigbus_queue; | |
79 | ||
80 | if (_likely_(c == 0)) | |
81 | return 0; | |
82 | ||
83 | if (_unlikely_(c >= SIGBUS_QUEUE_MAX)) | |
84 | return -EOVERFLOW; | |
85 | ||
86 | for (u = 0; u < SIGBUS_QUEUE_MAX; u++) { | |
87 | void *addr; | |
88 | ||
89 | addr = sigbus_queue[u]; | |
90 | if (!addr) | |
91 | continue; | |
92 | ||
93 | if (__sync_bool_compare_and_swap(&sigbus_queue[u], addr, NULL)) { | |
94 | __sync_fetch_and_sub(&n_sigbus_queue, 1); | |
95 | *ret = addr; | |
96 | return 1; | |
97 | } | |
98 | } | |
99 | } | |
100 | } | |
101 | ||
102 | static void sigbus_handler(int sn, siginfo_t *si, void *data) { | |
103 | unsigned long ul; | |
104 | void *aligned; | |
105 | ||
106 | assert(sn == SIGBUS); | |
107 | assert(si); | |
108 | ||
109 | if (si->si_code != BUS_ADRERR || !si->si_addr) { | |
110 | assert_se(sigaction(SIGBUS, &old_sigaction, NULL) == 0); | |
111 | raise(SIGBUS); | |
112 | return; | |
113 | } | |
114 | ||
115 | ul = (unsigned long) si->si_addr; | |
116 | ul = ul / page_size(); | |
117 | ul = ul * page_size(); | |
118 | aligned = (void*) ul; | |
119 | ||
120 | /* Let's remember which address failed */ | |
121 | sigbus_push(aligned); | |
122 | ||
123 | /* Replace mapping with an anonymous page, so that the | |
124 | * execution can continue, however with a zeroed out page */ | |
125 | assert_se(mmap(aligned, page_size(), PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS|MAP_FIXED, -1, 0) == aligned); | |
126 | } | |
127 | ||
128 | void sigbus_install(void) { | |
129 | struct sigaction sa = { | |
130 | .sa_sigaction = sigbus_handler, | |
131 | .sa_flags = SA_SIGINFO, | |
132 | }; | |
133 | ||
134 | n_installed++; | |
135 | ||
136 | if (n_installed == 1) | |
137 | assert_se(sigaction(SIGBUS, &sa, &old_sigaction) == 0); | |
138 | ||
139 | return; | |
140 | } | |
141 | ||
142 | void sigbus_reset(void) { | |
143 | ||
144 | if (n_installed <= 0) | |
145 | return; | |
146 | ||
147 | n_installed--; | |
148 | ||
149 | if (n_installed == 0) | |
150 | assert_se(sigaction(SIGBUS, &old_sigaction, NULL) == 0); | |
151 | ||
152 | return; | |
153 | } |