-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmwmon-2.6.24.2-twopackages.patch
586 lines (581 loc) · 19.8 KB
/
mwmon-2.6.24.2-twopackages.patch
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
diff -ruN linux-2.6.24.2/.config linux-2.6.24.2-anastop/.config
--- linux-2.6.24.2/.config 2008-11-05 19:56:19.000000000 +0200
+++ linux-2.6.24.2-anastop/.config 2008-11-05 18:26:49.000000000 +0200
@@ -1,7 +1,7 @@
#
# Automatically generated make config: don't edit
# Linux kernel version: 2.6.24.2
-# Wed Nov 5 19:56:19 2008
+# Wed Nov 5 18:26:49 2008
#
CONFIG_64BIT=y
# CONFIG_X86_32 is not set
diff -ruN linux-2.6.24.2/.config.old linux-2.6.24.2-anastop/.config.old
--- linux-2.6.24.2/.config.old 2008-11-05 19:56:04.000000000 +0200
+++ linux-2.6.24.2-anastop/.config.old 2008-11-05 18:25:47.000000000 +0200
@@ -1,7 +1,7 @@
#
# Automatically generated make config: don't edit
# Linux kernel version: 2.6.24.2
-# Wed Nov 5 19:56:04 2008
+# Wed Nov 5 18:23:46 2008
#
CONFIG_64BIT=y
# CONFIG_X86_32 is not set
@@ -183,7 +183,7 @@
CONFIG_NR_CPUS=8
CONFIG_SCHED_SMT=y
CONFIG_SCHED_MC=y
-# CONFIG_PREEMPT_NONE is not set
+#CONFIG_PREEMPT_NONE=y
# CONFIG_PREEMPT_VOLUNTARY is not set
CONFIG_PREEMPT=y
CONFIG_PREEMPT_BKL=y
@@ -1811,7 +1811,6 @@
# CONFIG_SCHEDSTATS is not set
# CONFIG_TIMER_STATS is not set
# CONFIG_SLUB_DEBUG_ON is not set
-CONFIG_DEBUG_PREEMPT=y
# CONFIG_DEBUG_RT_MUTEXES is not set
# CONFIG_RT_MUTEX_TESTER is not set
# CONFIG_DEBUG_SPINLOCK is not set
diff -ruN linux-2.6.24.2/Module.symvers linux-2.6.24.2-anastop/Module.symvers
--- linux-2.6.24.2/Module.symvers 2008-11-05 20:06:03.000000000 +0200
+++ linux-2.6.24.2-anastop/Module.symvers 2008-11-05 18:48:02.000000000 +0200
@@ -340,6 +340,7 @@
0x983f4d34 sock_wfree vmlinux EXPORT_SYMBOL
0x83cedcf5 test_set_page_writeback vmlinux EXPORT_SYMBOL
0xcd22f083 __page_cache_alloc vmlinux EXPORT_SYMBOL
+0x7cb9e23c sys_cpu_halt vmlinux EXPORT_SYMBOL_GPL
0x091eb9b4 round_jiffies vmlinux EXPORT_SYMBOL_GPL
0x68338e80 dm_destroy_dirty_log drivers/md/dm-mirror EXPORT_SYMBOL
0x18acd0d8 qdisc_destroy vmlinux EXPORT_SYMBOL
@@ -403,6 +404,7 @@
0xd88e433d register_sysctl_table vmlinux EXPORT_SYMBOL
0x5ef8a9a1 tty_flip_buffer_push vmlinux EXPORT_SYMBOL
0xeb1bf9ab uart_get_divisor vmlinux EXPORT_SYMBOL
+0x621acea4 mwmon_mmap_area2 vmlinux EXPORT_SYMBOL_GPL
0xf9a482f9 msleep vmlinux EXPORT_SYMBOL
0x5355f208 unregister_netdev vmlinux EXPORT_SYMBOL
0x029d9f89 __bforget vmlinux EXPORT_SYMBOL
@@ -1034,6 +1036,7 @@
0x72a86569 seq_release vmlinux EXPORT_SYMBOL
0x184e7087 parport_register_driver drivers/parport/parport EXPORT_SYMBOL
0xb92ebdf0 hidinput_disconnect vmlinux EXPORT_SYMBOL_GPL
+0xcc0b7595 sys_mwmon_mmap_sleep vmlinux EXPORT_SYMBOL_GPL
0xd6feefa5 agp_num_entries vmlinux EXPORT_SYMBOL_GPL
0x35a723ff __blk_put_request vmlinux EXPORT_SYMBOL_GPL
0xbacefb77 elv_rb_latter_request vmlinux EXPORT_SYMBOL
@@ -1059,6 +1062,7 @@
0x531c277b ide_proc_unregister_driver vmlinux EXPORT_SYMBOL
0x79aa05a8 match_token vmlinux EXPORT_SYMBOL
0x29a156bf vmtruncate vmlinux EXPORT_SYMBOL
+0x3b007a12 mwmon_mmap_area vmlinux EXPORT_SYMBOL_GPL
0x7e5648a5 down_write vmlinux EXPORT_SYMBOL
0x0d7d7986 compat_ip_getsockopt vmlinux EXPORT_SYMBOL
0xf5f2f6c3 ip_route_output_flow vmlinux EXPORT_SYMBOL_GPL
@@ -3489,6 +3493,7 @@
0x0130a68c simple_attr_open vmlinux EXPORT_SYMBOL_GPL
0x0946b75a shrink_submounts vmlinux EXPORT_SYMBOL_GPL
0xd66b15f4 usb_root_hub_lost_power drivers/usb/core/usbcore EXPORT_SYMBOL_GPL
+0xb2f37741 sys_mwmon_mmap_sleep2 vmlinux EXPORT_SYMBOL_GPL
0x85c10896 rcu_batches_completed_bh vmlinux EXPORT_SYMBOL_GPL
0xcc7fa952 local_bh_enable_ip vmlinux EXPORT_SYMBOL
0xbea9330c xfrm4_rcv_encap vmlinux EXPORT_SYMBOL
diff -ruN linux-2.6.24.2/include/asm/unistd_64.h linux-2.6.24.2-anastop/include/asm/unistd_64.h
--- linux-2.6.24.2/include/asm/unistd_64.h 2008-02-11 07:51:11.000000000 +0200
+++ linux-2.6.24.2-anastop/include/asm/unistd_64.h 2008-11-05 18:17:53.000000000 +0200
@@ -635,6 +635,13 @@
__SYSCALL(__NR_eventfd, sys_eventfd)
#define __NR_fallocate 285
__SYSCALL(__NR_fallocate, sys_fallocate)
+#define __NR_cpu_halt 286
+__SYSCALL(__NR_cpu_halt, sys_cpu_halt)
+#define __NR_mwmon_mmap_sleep 287
+__SYSCALL(__NR_mwmon_mmap_sleep, sys_mwmon_mmap_sleep)
+#define __NR_mwmon_mmap_sleep2 288
+__SYSCALL(__NR_mwmon_mmap_sleep2, sys_mwmon_mmap_sleep2)
+
#ifndef __NO_STUBS
#define __ARCH_WANT_OLD_READDIR
diff -ruN linux-2.6.24.2/include/asm-x86/unistd_64.h linux-2.6.24.2-anastop/include/asm-x86/unistd_64.h
--- linux-2.6.24.2/include/asm-x86/unistd_64.h 2008-02-11 07:51:11.000000000 +0200
+++ linux-2.6.24.2-anastop/include/asm-x86/unistd_64.h 2008-11-05 18:17:53.000000000 +0200
@@ -635,6 +635,13 @@
__SYSCALL(__NR_eventfd, sys_eventfd)
#define __NR_fallocate 285
__SYSCALL(__NR_fallocate, sys_fallocate)
+#define __NR_cpu_halt 286
+__SYSCALL(__NR_cpu_halt, sys_cpu_halt)
+#define __NR_mwmon_mmap_sleep 287
+__SYSCALL(__NR_mwmon_mmap_sleep, sys_mwmon_mmap_sleep)
+#define __NR_mwmon_mmap_sleep2 288
+__SYSCALL(__NR_mwmon_mmap_sleep2, sys_mwmon_mmap_sleep2)
+
#ifndef __NO_STUBS
#define __ARCH_WANT_OLD_READDIR
diff -ruN linux-2.6.24.2/include/config/auto.conf linux-2.6.24.2-anastop/include/config/auto.conf
--- linux-2.6.24.2/include/config/auto.conf 2008-11-05 19:56:35.000000000 +0200
+++ linux-2.6.24.2-anastop/include/config/auto.conf 2008-11-05 18:26:49.000000000 +0200
@@ -1,7 +1,7 @@
#
# Automatically generated make config: don't edit
# Linux kernel version: 2.6.24.2
-# Wed Nov 5 19:56:35 2008
+# Wed Nov 5 18:26:49 2008
#
CONFIG_VIDEO_V4L1_COMPAT=y
CONFIG_BLK_DEV_FD=m
diff -ruN linux-2.6.24.2/include/linux/autoconf.h linux-2.6.24.2-anastop/include/linux/autoconf.h
--- linux-2.6.24.2/include/linux/autoconf.h 2008-11-05 19:56:35.000000000 +0200
+++ linux-2.6.24.2-anastop/include/linux/autoconf.h 2008-11-05 18:26:49.000000000 +0200
@@ -1,7 +1,7 @@
/*
* Automatically generated C config: don't edit
* Linux kernel version: 2.6.24.2
- * Wed Nov 5 19:56:35 2008
+ * Wed Nov 5 18:26:49 2008
*/
#define AUTOCONF_INCLUDED
#define CONFIG_VIDEO_V4L1_COMPAT 1
diff -ruN linux-2.6.24.2/include/linux/cpu_controls.h linux-2.6.24.2-anastop/include/linux/cpu_controls.h
--- linux-2.6.24.2/include/linux/cpu_controls.h 1970-01-01 02:00:00.000000000 +0200
+++ linux-2.6.24.2-anastop/include/linux/cpu_controls.h 2008-11-05 17:51:21.000000000 +0200
@@ -0,0 +1,21 @@
+#ifndef __CPU_CONTROLS_H
+#define __CPU_CONTROLS_H
+
+extern char *mwmon_mmap_area;
+extern char *mwmon_mmap_area2;
+
+#if 0
+extern long int *mwmon_lint_var_addr;
+#endif
+
+#define MWMON_MONITORED_MEM_BYTES 64 //apo thn CPUID (input: eax=0x05)
+#define MWMON_ALIGN_BOUNDARY 64
+#define MWMON_ORIGINAL_VAL 'a'
+#define MWMON_NOTIFIED_VAL 'b'
+
+#if 0
+#define MWMON_LINT_VAR_OFFSET 128
+#endif
+
+#endif
+
diff -ruN linux-2.6.24.2/include/linux/syscalls.h linux-2.6.24.2-anastop/include/linux/syscalls.h
--- linux-2.6.24.2/include/linux/syscalls.h 2008-02-11 07:51:11.000000000 +0200
+++ linux-2.6.24.2-anastop/include/linux/syscalls.h 2008-11-05 18:20:58.000000000 +0200
@@ -611,6 +611,9 @@
const struct itimerspec __user *utmr);
asmlinkage long sys_eventfd(unsigned int count);
asmlinkage long sys_fallocate(int fd, int mode, loff_t offset, loff_t len);
+asmlinkage long sys_cpu_halt(void);
+asmlinkage long sys_mwmon_mmap_sleep(void);
+asmlinkage long sys_mwmon_mmap_sleep2(void);
int kernel_execve(const char *filename, char *const argv[], char *const envp[]);
diff -ruN linux-2.6.24.2/kernel/Makefile linux-2.6.24.2-anastop/kernel/Makefile
--- linux-2.6.24.2/kernel/Makefile 2008-02-11 07:51:11.000000000 +0200
+++ linux-2.6.24.2-anastop/kernel/Makefile 2008-11-05 18:23:03.000000000 +0200
@@ -9,7 +9,7 @@
rcupdate.o extable.o params.o posix-timers.o \
kthread.o wait.o kfifo.o sys_ni.o posix-cpu-timers.o mutex.o \
hrtimer.o rwsem.o latency.o nsproxy.o srcu.o \
- utsname.o notifier.o
+ utsname.o notifier.o cpu_controls.o
obj-$(CONFIG_SYSCTL) += sysctl_check.o
obj-$(CONFIG_STACKTRACE) += stacktrace.o
diff -ruN linux-2.6.24.2/kernel/cpu_controls.c linux-2.6.24.2-anastop/kernel/cpu_controls.c
--- linux-2.6.24.2/kernel/cpu_controls.c 1970-01-01 02:00:00.000000000 +0200
+++ linux-2.6.24.2-anastop/kernel/cpu_controls.c 2008-11-05 18:30:39.000000000 +0200
@@ -0,0 +1,390 @@
+#include <linux/cpu_controls.h>
+#include <linux/init.h>
+#include <linux/kernel.h> /* printk() */
+#include <linux/module.h>
+
+/*
+ * Pointers within memory allocated by device driver.
+ * This memory is mapped to userspace.
+ * The first pointer will be used by mwmon_mmap_sleep to
+ * exchange contents of first monitored memory region
+ * (assosiated with first physical package) with userspace,
+ * and the second will be used by mwmon_mmap_sleep2 to
+ * exchange contents of second monitored memory region
+ * (associated with second physical package) with userspace.
+ * _______
+ * | |
+ * Package1 <-> MemRegion1-->|*****| mwmon_mmap_area
+ * | |
+ * Package2 <-> MemRegion2-->|*****| mwmon_mmap_area2
+ * |_____|
+ * PAGE
+ *
+ */
+char *mwmon_mmap_area;
+EXPORT_SYMBOL_GPL(mwmon_mmap_area);
+
+char *mwmon_mmap_area2;
+EXPORT_SYMBOL_GPL(mwmon_mmap_area2);
+
+
+asmlinkage long sys_cpu_halt(void)
+{
+ //int i;
+
+ __asm__ __volatile__("sti; hlt"::);
+
+ //printk(KERN_ALERT "Halt! ");
+
+ //for(i=0; i<10; i++)
+ // printk(KERN_ALERT "%c", mwmon_mmap_area[i]);
+ //printk(KERN_ALERT "\n");
+
+ return 0;
+}
+EXPORT_SYMBOL_GPL(sys_cpu_halt);
+
+/*
+ *
+ * MWAIT-MONITOR stuff
+ *
+ */
+
+#define MWMON_MONITORED_MEM_BYTES 64 //apo thn CPUID (input: eax=0x05)
+#define MWMON_ALIGN_BOUNDARY 64
+#define MWMON_ORIGINAL_VAL 'a'
+#define MWMON_NOTIFIED_VAL 'b'
+
+static inline void monitor(const void *eax, unsigned long ecx,
+ unsigned long edx)
+{
+ /* "monitor %eax,%ecx,%edx;" */
+ asm volatile(
+ ".byte 0x0f,0x01,0xc8;"
+ :
+ :"a" (eax), "c" (ecx), "d"(edx));
+}
+
+static inline void mwait(unsigned long eax, unsigned long ecx)
+{
+ /* "mwait %eax,%ecx;" */
+ asm volatile(
+ ".byte 0x0f,0x01,0xc9;"
+ :
+ :"a" (eax), "c" (ecx));
+}
+
+
+/*
+ * kernelspace access for monitor (while in kernelspace),
+ * userspace access for notification (while in userspace)
+ */
+
+/*
+ * System call to be executed on first physical package
+ */
+asmlinkage long sys_mwmon_mmap_sleep(void)
+{
+ /* printk(KERN_ALERT "[mwmon_mmap_sleep]: Going to monitor memory location: %p)\n", mwmon_mmap_area); */
+
+ do {
+ local_irq_disable();
+ monitor(mwmon_mmap_area,0,0);
+ local_irq_enable();
+
+ if(*mwmon_mmap_area == MWMON_NOTIFIED_VAL)
+ break;
+
+ /*__asm__ __volatile__("pause":::"memory");*/
+
+ mwait(0,0);
+ } while (*mwmon_mmap_area != MWMON_NOTIFIED_VAL);
+
+ /* printk(KERN_ALERT "[mwmon_mmap_sleep]: Got out of spin loop... \n"); */
+
+ *mwmon_mmap_area = MWMON_ORIGINAL_VAL;
+
+ return 0;
+}
+EXPORT_SYMBOL_GPL(sys_mwmon_mmap_sleep);
+
+
+/*
+ * System call to be executed on second physical package
+ */
+asmlinkage long sys_mwmon_mmap_sleep2(void)
+{
+ /* printk(KERN_ALERT "[mwmon_mmap_sleep2]: Going to monitor memory location: %p)\n", mwmon_mmap_area2); */
+
+ do {
+ local_irq_disable();
+ monitor(mwmon_mmap_area2,0,0);
+ local_irq_enable();
+
+ if(*mwmon_mmap_area2 == MWMON_NOTIFIED_VAL)
+ break;
+
+ /*__asm__ __volatile__("pause":::"memory");*/
+
+ mwait(0,0);
+ } while (*mwmon_mmap_area2 != MWMON_NOTIFIED_VAL);
+
+ /* printk(KERN_ALERT "[mwmon_mmap_sleep2]: Got out of spin loop... \n"); */
+
+ *mwmon_mmap_area2 = MWMON_ORIGINAL_VAL;
+
+ return 0;
+}
+EXPORT_SYMBOL_GPL(sys_mwmon_mmap_sleep2);
+
+
+
+#if 0
+/*
+ * userspace access for monitor (while in kernelspace),
+ * userspace access for notification (while in userspace)
+ */
+
+//Initialization, notification and destruction: from userspace
+
+/*
+ * mwmon_user_memory has to be aligned in user-space at MWMON_ALIGN_BOUNDARY boundary
+ */
+asmlinkage long sys_mwmon_user_sleep(char* mwmon_user_memory)
+{
+ //printk(KERN_ALERT "[mwmon_user_sleep]: Going to monitor memory location: %p)\n", mwmon_user_memory);
+
+ do {
+ local_irq_disable();
+ monitor((char __user *)mwmon_user_memory,0,0);
+ local_irq_enable();
+
+ if(*(char __user *)mwmon_user_memory == MWMON_NOTIFIED_VAL)
+ break;
+ /*__asm__ __volatile__("pause":::"memory");*/
+
+ mwait(0,0);
+ } while (*(char __user *)mwmon_user_memory != MWMON_NOTIFIED_VAL);
+
+ //printk(KERN_ALERT "[mwmon_user_sleep]: Got out of spin loop... \n");
+
+ *(char __user *)mwmon_user_memory = MWMON_ORIGINAL_VAL;
+
+ return 0;
+}
+
+
+
+
+
+/*
+ * kernelspace access for monitor (while in kernelspace),
+ * kernelspace access for notification (while in userspace)
+ */
+static char *__mwmon_kernel_memory;
+
+static void *my_kmalloc_align(int alignment, int size, int flags)
+{
+ void *p;
+
+ if (!(p = kmalloc(size + alignment - 1, flags))) {
+ //printk(KERN_ALERT "kmalloc: Could not allocate kernel memory\n");
+ return (void *) 0;
+ }
+ //Perform the actual alignment: 'alignment' must be a power of 2
+ p = (void *) (((unsigned long) p + alignment - 1) & ~(alignment - 1));
+
+ return p;
+
+}
+
+asmlinkage long sys_mwmon_kernel_init(void)
+{
+ int retval;
+
+ __mwmon_kernel_memory = my_kmalloc_align(MWMON_ALIGN_BOUNDARY, MWMON_MONITORED_MEM_BYTES * sizeof(char), GFP_KERNEL);
+ if (!__mwmon_kernel_memory) {
+ retval = -ENOMEM;
+ return retval;
+ }
+ memset(__mwmon_kernel_memory, 'a', MWMON_MONITORED_MEM_BYTES * sizeof(char));
+ //printk(KERN_ALERT "[mwmon_kernel_init]: Init OK (starting addr: %p)\n", &__mwmon_kernel_memory[0]);
+ //printk(KERN_ALERT "[mwmon_kenrel_init]: Initialized monitored memory location to %c\n", __mwmon_kernel_memory[0]);
+
+ return 0;
+}
+
+#if 0
+asmlinkage long sys_mwmon_kernel_sleep(void)
+{
+ //printk(KERN_ALERT "[mwmon_kernel_sleep]: Going to monitor memory location: %p)\n", &mwmon_memory[0]);
+ /*local_irq_enable(); */
+ if (__mwmon_kernel_memory[0] != MWMON_NOTIFIED_VAL) {
+ do {
+ local_irq_disable();
+ monitor((void*)&__mwmon_kernel_memory[0],0,0);
+ local_irq_enable();
+
+ /*if (__mwmon_kernel_memory[0] == MWMON_NOTIFIED_VAL)*/
+ /*break;*/
+
+ /*__asm__ __volatile__("pause":::"memory");*/
+
+ /*printk(KERN_ALERT "[mwmon_kernel_sleep]: ...in busy loop: %c\n", __mwmon_kernel_memory[0]);*/
+
+ mwait(0,0);
+ } while (__mwmon_kernel_memory[0] != MWMON_NOTIFIED_VAL);
+
+ printk(KERN_ALERT "[mwmon_kernel_sleep]: Got out of spin-loop\n");
+ } else
+ printk(KERN_ALERT "[mwmon_kernel_sleep]: Already notified, did not get into spin loop!\n");
+
+ __mwmon_kernel_memory[0] = MWMON_ORIGINAL_VAL;
+
+ return 0;
+}
+#endif
+
+
+asmlinkage long sys_mwmon_kernel_sleep(void)
+{
+ //printk(KERN_ALERT "[mwmon_kernel_sleep]: Going to monitor memory location: %p)\n", &__mwmon_kernel_memory[0]);
+ /*local_irq_enable(); */
+ do {
+ local_irq_disable();
+ monitor((void*)&__mwmon_kernel_memory[0],0,0);
+ local_irq_enable();
+
+ if (__mwmon_kernel_memory[0] == MWMON_NOTIFIED_VAL)
+ break;
+
+ /*__asm__ __volatile__("pause":::"memory");*/
+
+ /*printk(KERN_ALERT "[mwmon_kernel_sleep]: ...in busy loop: %c\n", __mwmon_kernel_memory[0]);*/
+
+ mwait(0,0);
+ } while (__mwmon_kernel_memory[0] != MWMON_NOTIFIED_VAL);
+
+ //printk(KERN_ALERT "[mwmon_kernel_sleep]: Got out of spin-loop\n");
+
+ __mwmon_kernel_memory[0] = MWMON_ORIGINAL_VAL;
+
+ return 0;
+}
+
+asmlinkage long sys_mwmon_kernel_notify(void)
+{
+ //printk(KERN_ALERT "[mwmon_kernel_notify]: Updating memory location %p)\n", &__mwmon_kernel_memory[0]);
+ __mwmon_kernel_memory[0] = MWMON_NOTIFIED_VAL;
+ //printk(KERN_ALERT "[mwmon_kernel_notify]: Updated monitored memory location to %c\n", __mwmon_kernel_memory[0]);
+
+ return 0;
+}
+
+asmlinkage long sys_mwmon_kernel_finalize(void)
+{
+ kfree(__mwmon_kernel_memory);
+ //printk(KERN_ALERT "[mwmon_kernel_finalize]: Freed monitored memory location!\n");
+
+ return 0;
+}
+
+
+
+/*
+ * other synch. mechanisms using MWAIT-MONITOR
+ *
+ */
+
+/* Offset (from the beginning of shared page) at which an 'long int'
+ * variable must be mapped (in userspace and kernelspace), in order to share it
+ * in kernelspace and userspace.
+ * Since we have already implemented the mechanism for mapping a whole page in
+ * kernel virtual address space to userspace, it would be 'overkill' to repeat a
+ * separate -yet similar- procedure for other variables we would like to share
+ * between kernel and userspace for other purposes than mwait/monitor functionality.
+ * So, we must take care not to place such variables in locations within the
+ * shared page, the update of which would trigger false wake-ups.
+ * 128 bytes from the beginning of the shared memory area (which designates the
+ * beginning of the memory area to monitor, as well) is a safe distance to prevent
+ * such false wake-ups. */
+#define MWMON_LINT_VAR_OFFSET 128
+
+//We define and export the variable address itself (although just the above offset
+//from the beginning of the shared page would suffice) so that we don't have to compute
+//the address every time we execute the system call. Instead, we can initialize
+//the address only once [ mwmon_lint_var_addr = (long int*)(mwmon_mmap_area + OFFSET); ]
+//, at module initialization phase.
+
+long int *mwmon_lint_var_addr;
+EXPORT_SYMBOL_GPL(mwmon_lint_var_addr);
+
+
+#if 0
+#define SPIN_ON_CONDITION(spin_var, condition_val) { \
+ __asm__ __volatile__ ("\n" \
+ "cmpl %1, %0\n" \
+ "je 2f\n" \
+ "1:\tpause\n\t" \
+ "cmpl %1, %0\n\t" \
+ "jne 1b\n" \
+ "2:\t\n" \
+ : \
+ :"m"(spin_var), "ir"(condition_val) \
+ ); \
+}
+
+//if ( spin_var >= condition_val ) { exit; } else { spin; }
+#define SPIN_ON_CONDITION_INEQ(spin_var, condition_val ) { \
+ __asm__ __volatile__ ("\n" \
+ "cmpl %1, %0\n" \
+ "jge 2f\n" \
+ "1:\tpause\n\t" \
+ "cmpl %1, %0\n\t" \
+ "jle 1b\n" \
+ "2:\t\n" \
+ : :"m"(spin_var), "ib"(condition_val) ); }
+#endif
+
+/*
+ * if ( spin_var >= condition_val ) { exit; } else { spin; }
+ *
+ * spin while local condition is greater than a global spin variable
+ */
+ asmlinkage long sys_mwmon_mmap_spin_while_greater(long int local_condition)
+{
+
+ /*
+ * If the preceding MONITOR instruction did not successfully arm an
+ * address range or if the MONITOR instruction has not been executed
+ * prior to executing MWAIT, then the processor will not enter the
+ * implementation-dependent-optimized state.
+ *
+ * In this scenario we actually do not monitor the specified address range
+ * for writes. We expect that mwait will break due to interrupts, since
+ * we do not assume that "notifier" thread will wake the "sleeper" up by
+ * writing to the monitored address.
+ *
+ * In case we would like the "notifier" to wake the "sleeper" up upon
+ * condition satisfaction, we should assign condition check to "notifier",
+ * or we should find a more clever way for simultaneous condition check
+ * and notification.
+ */
+
+ while ( (*mwmon_lint_var_addr) < local_condition ) {
+ local_irq_disable();
+ monitor(mwmon_mmap_area,0,0);
+ local_irq_enable();
+
+ //if(*mwmon_mmap_area == MWMON_NOTIFIED_VAL)
+ // break;
+
+ mwait(0,0);
+ }
+
+ *mwmon_mmap_area = MWMON_ORIGINAL_VAL;
+
+ return 0;
+}
+
+#endif