summaryrefslogtreecommitdiff
path: root/os/busfault.c
blob: a2d433a2e0ee316e5dc2c7399eec09a79390e1b4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
 * Copyright © 2013 Keith Packard
 *
 * Permission to use, copy, modify, distribute, and sell this software and its
 * documentation for any purpose is hereby granted without fee, provided that
 * the above copyright notice appear in all copies and that both that copyright
 * notice and this permission notice appear in supporting documentation, and
 * that the name of the copyright holders not be used in advertising or
 * publicity pertaining to distribution of the software without specific,
 * written prior permission.  The copyright holders make no representations
 * about the suitability of this software for any purpose.  It is provided "as
 * is" without express or implied warranty.
 *
 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
 * OF THIS SOFTWARE.
 */

#ifdef HAVE_DIX_CONFIG_H
#include <dix-config.h>
#endif

#include <X11/Xos.h>
#include <X11/Xdefs.h>
#include "misc.h"
#include <busfault.h>
#include <list.h>
#include <stddef.h>
#include <stdlib.h>
#include <stdint.h>
#include <sys/mman.h>
#include <signal.h>

struct busfault {
    struct xorg_list    list;

    void                *addr;
    size_t              size;

    Bool                valid;

    busfault_notify_ptr notify;
    void                *context;
};

static Bool             busfaulted;
static struct xorg_list busfaults;

struct busfault *
busfault_register_mmap(void *addr, size_t size, busfault_notify_ptr notify, void *context)
{
    struct busfault     *busfault;

    busfault = calloc(1, sizeof (struct busfault));
    if (!busfault)
        return NULL;

    busfault->addr = addr;
    busfault->size = size;
    busfault->notify = notify;
    busfault->context = context;
    busfault->valid = TRUE;

    xorg_list_add(&busfault->list, &busfaults);
    return busfault;
}

void
busfault_unregister(struct busfault *busfault)
{
    xorg_list_del(&busfault->list);
    free(busfault);
}

void
busfault_check(void)
{
    struct busfault     *busfault, *tmp;

    if (!busfaulted)
        return;

    busfaulted = FALSE;

    xorg_list_for_each_entry_safe(busfault, tmp, &busfaults, list) {
        if (!busfault->valid)
            (*busfault->notify)(busfault->context);
    }
}

static void (*previous_busfault_sigaction)(int sig, siginfo_t *info, void *param);

static void
busfault_sigaction(int sig, siginfo_t *info, void *param)
{
    void                *fault = info->si_addr;
    struct busfault     *iter, *busfault = NULL;
    void                *new_addr;

    /* Locate the faulting address in our list of shared segments
     */
    xorg_list_for_each_entry(iter, &busfaults, list) {
	if ((char *) iter->addr <= (char *) fault && (char *) fault < (char *) iter->addr + iter->size) {
	    busfault = iter;
	    break;
	}
    }
    if (!busfault)
        goto panic;

    if (!busfault->valid)
        goto panic;

    busfault->valid = FALSE;
    busfaulted = TRUE;

    /* The client truncated the file; unmap the shared file, map
     * /dev/zero over that area and keep going
     */

    new_addr = mmap(busfault->addr, busfault->size, PROT_READ|PROT_WRITE,
                    MAP_ANON|MAP_PRIVATE|MAP_FIXED, -1, 0);

    if (new_addr == MAP_FAILED)
        goto panic;

    return;
panic:
    if (previous_busfault_sigaction)
        (*previous_busfault_sigaction)(sig, info, param);
    else
        FatalError("bus error\n");
}

Bool
busfault_init(void)
{
    struct sigaction    act, old_act;

    act.sa_sigaction = busfault_sigaction;
    act.sa_flags = SA_SIGINFO;
    sigemptyset(&act.sa_mask);
    if (sigaction(SIGBUS, &act, &old_act) < 0)
        return FALSE;
    previous_busfault_sigaction = old_act.sa_sigaction;
    xorg_list_init(&busfaults);
    return TRUE;
}