summaryrefslogtreecommitdiff
path: root/sys/lockdoc/log.c
blob: 9f63e3369589d5bb5d7dbabe3deef28c70a53e41 (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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#include <arch/x86/include/cpufunc.h>
#include <sys/lwp.h>
#include <sys/lockdebug.h>
#include <sys/lockdoc.h>

#ifdef LOCKDOC

struct log_action la_buffer;

#define LOCK_TYPE "kmutex_t"

// Mutexes are always considered write locks
inline void __mutex_enter(kmutex_t * lock, const char* file, int line, const char* func) {
	_mutex_enter(lock);
	lockdoc_log_lock(P_WRITE, lock, file, line, func, LOCK_TYPE, LOCK_NONE);
}

inline void __mutex_exit(kmutex_t * lock, const char* file, int line, const char* func) {
	lockdoc_log_lock(V_WRITE, lock, file, line, func, LOCK_TYPE, LOCK_NONE);
	_mutex_exit(lock);
}

inline void __mutex_spin_enter(kmutex_t * lock, const char* file, int line, const char* func) {
	_mutex_spin_enter(lock);
	lockdoc_log_lock(P_WRITE, lock, file, line, func, LOCK_TYPE, LOCK_NONE);
}

inline void __mutex_spin_exit(kmutex_t * lock, const char* file, int line, const char* func) {
	lockdoc_log_lock(V_WRITE, lock, file, line, func, LOCK_TYPE, LOCK_NONE);
	_mutex_spin_exit(lock);
}

inline int __mutex_tryenter(kmutex_t * lock, const char* file, int line, const char* func) {
	int enter = _mutex_tryenter(lock);
	if(enter != 0) {
		lockdoc_log_lock(P_WRITE, lock, file, line, func, LOCK_TYPE, LOCK_NONE);
	}
	return enter;
}

#undef LOCK_TYPE
#define LOCK_TYPE "krwlock_t"

inline int __rw_tryupgrade(krwlock_t * lock, const char* file, int line, const char* func) {
	int upgrade = _rw_tryupgrade(lock);
	if(upgrade != 0) {
		lockdoc_log_lock(V_READ, lock, file, line, func, LOCK_TYPE, LOCK_NONE);
		lockdoc_log_lock(P_WRITE, lock, file, line, func, LOCK_TYPE, LOCK_NONE);
	}
	return upgrade;
}

inline void __rw_downgrade(krwlock_t * lock, const char* file, int line, const char* func) {
	_rw_downgrade(lock);
	lockdoc_log_lock(V_WRITE, lock, file, line, func, LOCK_TYPE, LOCK_NONE);
	lockdoc_log_lock(P_READ, lock, file, line, func, LOCK_TYPE, LOCK_NONE);
}

inline void __rw_enter(krwlock_t * lock, const krw_t type, const char* file, int line, const char* func) {
	_rw_enter(lock, type);
	if(type == RW_READER)
		lockdoc_log_lock(P_READ, lock, file, line, func, LOCK_TYPE, LOCK_NONE);
	else
		lockdoc_log_lock(P_WRITE, lock, file, line, func, LOCK_TYPE, LOCK_NONE);
}

inline int __rw_tryenter(krwlock_t * lock, const krw_t type, const char* file, int line, const char* func) {
	int enter = _rw_tryenter(lock, type);
	if(enter != 0) {
		if(type == RW_READER)
			lockdoc_log_lock(P_READ, lock, file, line, func, LOCK_TYPE, LOCK_NONE);
		else
			lockdoc_log_lock(P_WRITE, lock, file, line, func, LOCK_TYPE, LOCK_NONE);
	}
	return enter;
}

inline void __rw_exit(krwlock_t * lock, const char* file, int line, const char* func) {
	/*
	 * See sys/sys/rwlock.h
	 * We cannot include this directly due to circular dependencies.
	 */
	if(rw_lock_op(lock) == RW_WRITER)
		lockdoc_log_lock(V_WRITE, lock, file, line, func, LOCK_TYPE, LOCK_NONE);
	else
		lockdoc_log_lock(V_READ, lock, file, line, func, LOCK_TYPE, LOCK_NONE);
	_rw_exit(lock);
}
#undef LOCK_TYPE

/*
 * Yet to be implemented: Check https://man.netbsd.org/locking.9 (esp. IRQ, CVlock and BKL, check issue tracker)
 */

int32_t lockdoc_get_ctx(void) {
	if (curlwp == NULL) {
		return 0;
	} else if (curlwp->l_pflag & LP_INTR) {
		return -1;
	} else {
		return curlwp->l_lid;
	}
}

void lockdoc_send_current_task_addr(void) {
	u_long flags;

	flags = lockdoc_x86_disable_intr();

	memset(&la_buffer,0,sizeof(la_buffer));

	la_buffer.action = LOCKDOC_CURRENT_TASK;
	la_buffer.ptr = (uint32_t)((uint32_t)curcpu()->ci_self + offsetof(struct cpu_info, ci_curlwp));

	outb_(PING_CHAR,IO_PORT_LOG);

	lockdoc_x86_restore_intr(flags);
}

void lockdoc_send_lwp_flag_offset(void) {
	u_long flags;

	flags = lockdoc_x86_disable_intr();

	memset(&la_buffer,0,sizeof(la_buffer));

	la_buffer.action = LOCKDOC_LWP_FLAG_OFFSET;
	la_buffer.ptr = offsetof(struct lwp, l_pflag);
	outb_(PING_CHAR,IO_PORT_LOG);

	lockdoc_x86_restore_intr(flags);
}

void lockdoc_send_pid_offset(void) {
	u_long flags;

	flags = lockdoc_x86_disable_intr();

	memset(&la_buffer,0,sizeof(la_buffer));

	la_buffer.action = LOCKDOC_PID_OFFSET;
	la_buffer.ptr = offsetof(struct lwp, l_lid);
	outb_(PING_CHAR,IO_PORT_LOG);

	lockdoc_x86_restore_intr(flags);
}

void lockdoc_send_kernel_version(void) {
	u_long flags;

	flags = lockdoc_x86_disable_intr();

	memset(&la_buffer,0,sizeof(la_buffer));

	snprintf((char*)&la_buffer.type, LOG_CHAR_BUFFER_LEN, "%s", "notimplemented"); // TODO Implement
	la_buffer.action = LOCKDOC_KERNEL_VERSION;
	outb_(PING_CHAR,IO_PORT_LOG);

	lockdoc_x86_restore_intr(flags);
}

void trace_irqs_on(struct trapframe *frame) {
	u_long cur_eflags;
	cur_eflags = x86_read_flags();
	if ((frame->tf_eflags & (1 << 9)) && !(cur_eflags & (1 << 9))) {
		lockdoc_log_lock(V_WRITE, (void *)PSEUDOLOCK_ADDR_HARDIRQ, __FILE__, __LINE__, "dummy", "dummy", LOCK_NONE);
	}
}

void trace_irqs_off(struct trapframe *frame) {
	u_long cur_eflags;
	cur_eflags = x86_read_flags();
	if ((frame->tf_eflags & (1 << 9)) && !(cur_eflags & (1 << 9))) {
		lockdoc_log_lock(P_WRITE, (void *)PSEUDOLOCK_ADDR_HARDIRQ, __FILE__, __LINE__, "dummy", "dummy", frame->tf_trapno);
	}
}

void __x86_disable_intr(const char *file, int line, const char *func){
    u_long eflags;
    
    eflags = x86_read_flags();
    if (eflags & (1 << 9)){  // Check if interrupts were enabled in the first place
		lockdoc_log_lock(P_WRITE, (void *)PSEUDOLOCK_ADDR_HARDIRQ, file, line, "dummy", "dummy", LOCK_NONE); 
    }
    lockdoc_x86_disable_intr();
}

u_long lockdoc_x86_disable_intr(void){
    u_long eflags;

    eflags = x86_read_flags();
	__asm volatile ("cli" ::: "memory");
    return eflags;

}

void __x86_enable_intr(const char *file, int line, const char *func){
    u_long eflags;
    
    eflags = x86_read_flags();
    if (!(eflags & (1 << 9))){  // Check if interrupts were disabled in the first place
		lockdoc_log_lock(V_WRITE, (void *)PSEUDOLOCK_ADDR_HARDIRQ, file, line, "dummy", "dummy", LOCK_NONE); 
    }
    lockdoc_x86_enable_intr();
}

u_long lockdoc_x86_enable_intr(void){
    u_long eflags;

    eflags = x86_read_flags();
	__asm volatile ("sti" ::: "memory");
    return eflags;
}

void lockdoc_x86_restore_intr(u_long eflags){
    x86_write_flags(eflags);
}

#endif /* LOCKDOC */