-
Notifications
You must be signed in to change notification settings - Fork 4
/
kml_4.0_001.diff
5501 lines (5312 loc) · 169 KB
/
kml_4.0_001.diff
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
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
diff --git a/CREDITS b/CREDITS
index 96935df..f6a1c03 100644
--- a/CREDITS
+++ b/CREDITS
@@ -2280,6 +2280,10 @@ S: PO BOX 220, HFX. CENTRAL
S: Halifax, Nova Scotia
S: Canada B3J 3C8
+N: Toshiyuki Maeda
+E: tosh@is.s.u-tokyo.ac.jp
+D: Kernel Mode Linux
+
N: Kai Mäkisara
E: Kai.Makisara@kolumbus.fi
D: SCSI Tape Driver
diff --git a/Documentation/00-INDEX b/Documentation/00-INDEX
index cd077ca..7bd8c5c 100644
--- a/Documentation/00-INDEX
+++ b/Documentation/00-INDEX
@@ -267,6 +267,8 @@ kmemcheck.txt
- info on dynamic checker that detects uses of uninitialized memory.
kmemleak.txt
- info on how to make use of the kernel memory leak detection system
+kml.txt
+ - info on Kernel Mode Linux.
ko_KR/
- directory with Korean translations of various documents
kobject.txt
diff --git a/Documentation/kml.txt b/Documentation/kml.txt
new file mode 100644
index 0000000..dab3958
--- /dev/null
+++ b/Documentation/kml.txt
@@ -0,0 +1,170 @@
+Kernel Mode Linux (http://web.yl.is.s.u-tokyo.ac.jp/~tosh/kml)
+Copyright 2004,2005 Toshiyuki Maeda
+
+
+Introduction:
+
+Kernel Mode Linux is a technology which enables us to execute user programs
+in kernel mode. In Kernel Mode Linux, user programs can be executed as
+user processes that have the privilege level of kernel mode. The benefit
+of executing user programs in kernel mode is that the user programs can
+access kernel address space directly. For example, user programs can invoke
+system calls very fast because it is unnecessary to switch between a kernel
+mode and user-mode by using costly software interruptions or context
+switches. In addition, user programs are executed as ordinary processes
+(except for their privilege level, of course), so scheduling and paging are
+performed as usual, unlike kernel modules.
+
+Although it seems dangerous to let user programs access a kernel directly,
+safety of the kernel can be ensured by several means: static type checking
+technology, proof-carrying code technology, software fault isolation, and
+so forth. For proof of concept, we are developing a system which is based
+on the combination of Kernel Mode Linux and Typed Assembly Language, TAL.
+(TAL can ensure safety of programs through its type checking and the type
+checking can be done at machine binary level. For more information about
+TAL, see http://www.cs.cornell.edu/talc)
+
+Currently, IA-32 and AMD64 architecture are supported.
+
+
+Limitation:
+User processes executed in kernel mode should obey the following limitations.
+Otherwise, your system will be in an undefined state. In the worst-case
+scenario, your system will crash.
+
+- On IA-32, user processes executed in kernel mode should not modify their
+ CS, DS, FS and SS register.
+
+- On AMD64, user processes executed in kernel mode should not modify their
+ GS register.
+
+In addition, on AMD64, IA-32 binaries cannot be executed in kernel mode.
+
+
+Instruction:
+
+To enable Kernel Mode Linux, say Y in Kernel Mode Linux field of kernel
+configuration, build and install the kernel, and reboot your machine. Then,
+all executables under the "/trusted" directory are executed in kernel mode
+in the current Kernel Mode Linux implementation. For example, to execute a
+program named "cat" in kernel mode, copy the program to "/trusted" and
+execute it as follows:
+
+% /trusted/cat
+
+
+Implementation Notes for IA-32:
+
+To execute user programs in kernel mode, Kernel Mode Linux has a special
+start_thread (start_kernel_thread) routine, which is called in processing
+execve(2) and sets registers of a user process to specified initial values.
+The original start_thread routine sets CS segment register to __USER_CS.
+The start_kernel_thread routine sets the CS register to __KERNEL_CS. Thus,
+a user program is started as a user process executed in kernel mode.
+
+The biggest problem of implementing Kernel Mode Linux is a stack starvation
+problem. Let's assume that a user program is executed in kernel mode and
+it causes a page fault on its user stack. To generate a page fault exception,
+an IA-32 CPU tries to push several registers (EIP, CS, and so on) to the same
+user stack because the program is executed in kernel mode and the IA-32
+CPU doesn't switch its stack to a kernel stack. Therefore, the IA-32 CPU
+cannot push the registers and generate a double fault exception and fail
+again. Finally, the IA-32 CPU gives up and reset itself. This is the stack
+starvation problem.
+
+To solve the stack starvation problem, we use the IA-32 hardware task mechanism
+to handle exceptions. By using the mechanism, IA-32 CPU doesn't push the
+registers to its stack. Instead, the CPU switches an execution context to
+another special context. Therefore, the stack starvation problem doesn't occur.
+However, it is costly to handle all exceptions by the IA-32 task mechanism.
+So, in current Kernel Mode Linux implementation, double fault exceptions are
+handled by the IA-32 task. A page fault on a memory stack is not so often, so
+the cost of the IA-32 task mechanism is negligible for usual programs.
+In addition, non-maskable interrupts are also handled by the IA-32 task.
+The reason is described later in this document.
+
+The second problem is a manual stack switching problem. In the original Linux
+kernel, an IA-32 CPU switches a stack from a user stack to a kernel stack on
+exceptions or interrupts. However, in Kernel Mode Linux, a user program
+may be executed in kernel mode and the CPU may not switch a stack.
+Therefore, in current Kernel Mode Linux implementation, the kernel switches
+a stack manually on exceptions and interrupts. To switch a stack, a kernel
+need to know a location of a kernel stack in an address space. However, on
+exceptions and interrupts, the kernel cannot use general registers (EAX, EBX,
+and so on). Therefore, it is very difficult to get the location of the kernel stack.
+
+To solve the above problem, the current Kernel Mode Linux implementation
+exploits a per CPU GDT. In Kernel Mode Linux, one segment descriptor of
+the per CPU GDT entries directly points to the location of the per-CPU TSS
+(Task State Segment). Thus, by using the segment descriptor, the address
+of the kernel stack can be available with only one general register.
+
+The third problem is an interrupt-lost problem on double fault exceptions.
+Let's assume that a user program is executed in kernel mode, and its ESP
+register points to a portion of memory space that has not been mapped to
+its address space yet. What will happen if an external interrupt is raised
+just in time? First, a CPU acks the request for the interrupt from an
+external interrupt controller. Then, the CPU tries to interrupt its execution
+of the user program. However, it can't because there is no stack to save
+the part of the execution context (see above "a stack starvation problem").
+Then, the CPU tries to generate a double fault exception and it succeeds
+because the Kernel Mode Linux implementation handles the double fault by the
+IA-32 task. The problem is that the double fault exception handler knows only
+the suspended user program and it cannot know the request for the interrupt
+because the CPU doesn't tell nothing about it. Therefore, the double fault
+handler directly resumes the user program and doesn't handle the interrupt,
+that is, the same kind of interrupts never be generated because the interrupt
+controller thinks that the previous interrupt has not been serviced by the CPU.
+
+To solve the interrupt-lost problem, the current Kernel Mode Linux implementation
+asks the interrupt controller for untreated interrupts and handles them at the
+end of the double fault exception handler. Asking the interrupt controller is a
+costly operation. However, the cost is negligible because double fault exceptions
+that is, page faults on memory stacks are not so often.
+
+The reason for handling non-maskable interrupts by the IA-32 tasks is closely
+related to the manual stack switching problem and the interrupt-lost problem.
+If an non-maskable interrupt occurs between when a maskable interrupt occurs and
+when a memory stack is switched from a user stack to a kernel stack, and the
+non-maskable interrupt causes a page fault on the memory stack, then the double
+fault exception handler handles the maskable interrupt because it has not been
+handled. The problem is that the double fault handler returns to the suspended
+interrupt handling routine and the routine tries to handle the already-handled
+maskable interrupt again.
+
+The above problem can be avoided by handling non-maskable interrupts with the
+IA-32 tasks, because no double fault exceptions are generated. Usually, non-maskable
+interrupts are very rare, so the cost of the IA-32 task mechanisms doesn't really
+matter. However, if an NMI watchdog is enabled for debugging purpose, performance
+degradation may be observed.
+
+One problem for handling non-maskable interrupts by the IA-32 task mechanism is
+a descriptor-tables inconsistency problem. When the IA-32 tasks are switched
+back and forth, all segment registers (CS, DS, ES, SS, FS, GS) and the local
+descriptor table register (LDTR) are reloaded (unlike the usual IA-32 trap/interrupt
+mechanism). Therefore, to switch the IA-32 task, the global descriptor table
+and the local descriptor table should be consistent, otherwise, the invalid TSS
+exception is raised and it is too complex to recover from the exception.
+The problem is that the consistency cannot be guaranteed because non-maskable
+interrupts are raised anytime and anywhere, that is, when updating the global
+descriptor table or the local descriptor table.
+
+To solve the above problem, the current Kernel Mode Linux implementation inserts
+instructions for saving and restoring FS, GS, and/or LDTR around the portion
+that manipulate the descriptor tables, if needed (CS, DS, ES are used exclusively
+by the kernel at that point, so there are no problems). Then, the non-maskable
+interrupt handler checks whether if FS, GS, and LDTR can be reloaded without problems,
+at the end of itself. If a problem is found, it reloads FS, GS, and/or LDTR with '0'
+(reloading FS, GS, and/or LDTR with '0' always succeeds). The reason why the above
+solution works is as follows. First, if a problem is found at reloading FS, GS,
+and/or LDTR, that means that a non-maskable interrupt occurs when modifying the
+descriptor tables. However, FS, GS, and/or LDTR are properly reloaded after the
+modification by the above mentioned instructions for restoring them. Therefore,
+just reloading FS, GS, and/or LDTR with '0' works because they will be reloaded
+soon after. Inserting the instructions may affect performance. Fortunately, however,
+FS, GS, and/or LDTR are usually reloaded after modifying the descriptor tables,
+so there are little points at that the instructions should be inserted.
+
+
+Implementation Notes for AMD64:
+(Now writing...)
diff --git a/MAINTAINERS b/MAINTAINERS
index efbcb50..7e35a3b 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -5560,6 +5560,12 @@ L: kernel-janitors@vger.kernel.org
W: http://kernelnewbies.org/KernelJanitors
S: Odd Fixes
+KERNEL MODE LINUX
+P: Toshiyuki Maeda
+M: tosh@is.s.u-tokyo.ac.jp
+W: http://www.yl.is.s.u-tokyo.ac.jp/~tosh/kml/
+S: Maintained
+
KERNEL NFSD, SUNRPC, AND LOCKD SERVERS
M: "J. Bruce Fields" <bfields@fieldses.org>
L: linux-nfs@vger.kernel.org
diff --git a/Makefile b/Makefile
index fbd43bf..e89d832 100644
--- a/Makefile
+++ b/Makefile
@@ -1,7 +1,7 @@
VERSION = 4
PATCHLEVEL = 0
SUBLEVEL = 0
-EXTRAVERSION =
+EXTRAVERSION = -kml
NAME = Hurr durr I'ma sheep
# *DOCUMENTATION*
diff --git a/arch/arm/Kconfig b/arch/arm/Kconfig
index cf4c0c9..cb95d5d 100644
--- a/arch/arm/Kconfig
+++ b/arch/arm/Kconfig
@@ -2159,6 +2159,10 @@ endmenu
source "net/Kconfig"
+if (MMU && !CPU_USE_DOMAINS)
+source "kernel/Kconfig.kml"
+endif
+
source "drivers/Kconfig"
source "fs/Kconfig"
diff --git a/arch/arm/include/asm/processor.h b/arch/arm/include/asm/processor.h
index 8a1e8e9..6914ffa 100644
--- a/arch/arm/include/asm/processor.h
+++ b/arch/arm/include/asm/processor.h
@@ -23,6 +23,9 @@
#include <asm/ptrace.h>
#include <asm/types.h>
#include <asm/unified.h>
+#ifdef CONFIG_KERNEL_MODE_LINUX
+#include <linux/thread_info.h>
+#endif
#ifdef __KERNEL__
#define STACK_TOP ((current->personality & ADDR_LIMIT_32BIT) ? \
@@ -53,6 +56,14 @@ struct thread_struct {
#define nommu_start_thread(regs) regs->ARM_r10 = current->mm->start_data
#endif
+#ifdef CONFIG_KERNEL_MODE_LINUX
+static inline void clear_thread_flag_ku(void) { clear_thread_flag(TIF_KU); }
+static inline void set_thread_flag_ku(void) { set_thread_flag(TIF_KU); }
+#else
+static inline void clear_thread_flag_ku(void) { return; }
+static inline void set_thread_flag_ku(void) { return; }
+#endif
+
#define start_thread(regs,pc,sp) \
({ \
memset(regs->uregs, 0, sizeof(regs->uregs)); \
@@ -66,7 +77,31 @@ struct thread_struct {
regs->ARM_pc = pc & ~1; /* pc */ \
regs->ARM_sp = sp; /* sp */ \
nommu_start_thread(regs); \
+ clear_thread_flag_ku(); \
+})
+
+#ifdef CONFIG_KERNEL_MODE_LINUX
+#define start_kernel_thread(regs,pc,sp) \
+({ \
+ unsigned long *stack = (unsigned long *)sp; \
+ set_fs(KERNEL_DS); \
+ memset(regs->uregs, 0, sizeof(regs->uregs)); \
+ if (current->personality & ADDR_LIMIT_32BIT) \
+ regs->ARM_cpsr = SYSTEM_MODE; \
+ else \
+ regs->ARM_cpsr = USR26_MODE; \
+ if (elf_hwcap & HWCAP_THUMB && pc & 1) \
+ regs->ARM_cpsr |= PSR_T_BIT; \
+ regs->ARM_cpsr |= PSR_ENDSTATE; \
+ regs->ARM_pc = pc & ~1; /* pc */ \
+ regs->ARM_sp = sp; /* sp */ \
+ regs->ARM_r2 = stack[2]; /* r2 (envp) */ \
+ regs->ARM_r1 = stack[1]; /* r1 (argv) */ \
+ regs->ARM_r0 = stack[0]; /* r0 (argc) */ \
+ nommu_start_thread(regs); \
+ set_thread_flag_ku(); \
})
+#endif
/* Forward declaration, a strange C thing */
struct task_struct;
diff --git a/arch/arm/include/asm/ptrace.h b/arch/arm/include/asm/ptrace.h
index 51622ba..6c40084 100644
--- a/arch/arm/include/asm/ptrace.h
+++ b/arch/arm/include/asm/ptrace.h
@@ -17,8 +17,15 @@ struct pt_regs {
unsigned long uregs[18];
};
+#ifdef CONFIG_KERNEL_MODE_LINUX
+static inline int test_thread_flag_ku(void);
+
+#define user_mode(regs) \
+ ((((regs)->ARM_cpsr & 0xf) == 0) || ((((regs)->ARM_cpsr & 0xf) == 0xf) && test_thread_flag_ku()))
+#else
#define user_mode(regs) \
(((regs)->ARM_cpsr & 0xf) == 0)
+#endif
#ifdef CONFIG_ARM_THUMB
#define thumb_mode(regs) \
@@ -60,6 +67,10 @@ static inline int valid_user_regs(struct pt_regs *regs)
if ((regs->ARM_cpsr & PSR_I_BIT) == 0) {
if (mode == USR_MODE)
return 1;
+#ifdef CONFIG_KERNEL_MODE_LINUX
+ if (mode == SYSTEM_MODE && test_thread_flag_ku())
+ return 1;
+#endif /* CONFIG_KERNEL_MODE_LINUX */
if (elf_hwcap & HWCAP_26BIT && mode == USR26_MODE)
return 1;
}
diff --git a/arch/arm/include/asm/thread_info.h b/arch/arm/include/asm/thread_info.h
index 72812a1..d5fea8a 100644
--- a/arch/arm/include/asm/thread_info.h
+++ b/arch/arm/include/asm/thread_info.h
@@ -159,6 +159,9 @@ extern int vfp_restore_user_hwstate(struct user_vfp __user *,
#define TIF_USING_IWMMXT 17
#define TIF_MEMDIE 18 /* is terminating due to OOM killer */
#define TIF_RESTORE_SIGMASK 20
+#ifdef CONFIG_KERNEL_MODE_LINUX
+#define TIF_KU 23
+#endif /* CONFIG_KERNEL_MODE_LINUX */
#define _TIF_SIGPENDING (1 << TIF_SIGPENDING)
#define _TIF_NEED_RESCHED (1 << TIF_NEED_RESCHED)
@@ -169,6 +172,9 @@ extern int vfp_restore_user_hwstate(struct user_vfp __user *,
#define _TIF_SYSCALL_TRACEPOINT (1 << TIF_SYSCALL_TRACEPOINT)
#define _TIF_SECCOMP (1 << TIF_SECCOMP)
#define _TIF_USING_IWMMXT (1 << TIF_USING_IWMMXT)
+#ifdef CONFIG_KERNEL_MODE_LINUX
+#define _TIF_KU (1 << TIF_KU)
+#endif /* CONFIG_KERNEL_MODE_LINUX */
/* Checks for any syscall work in entry-common.S */
#define _TIF_SYSCALL_WORK (_TIF_SYSCALL_TRACE | _TIF_SYSCALL_AUDIT | \
@@ -180,5 +186,15 @@ extern int vfp_restore_user_hwstate(struct user_vfp __user *,
#define _TIF_WORK_MASK (_TIF_NEED_RESCHED | _TIF_SIGPENDING | \
_TIF_NOTIFY_RESUME | _TIF_UPROBE)
+#ifndef __ASSEMBLY__
+#ifdef CONFIG_KERNEL_MODE_LINUX
+static inline int test_ti_thread_flag(struct thread_info *ti, int flag);
+static inline int test_thread_flag_ku(void)
+{
+ return test_ti_thread_flag(current_thread_info(), TIF_KU);
+}
+#endif /* CONFIG_KERNEL_MODE_LINUX */
+#endif /* __ASSEMBLY__ */
+
#endif /* __KERNEL__ */
#endif /* __ASM_ARM_THREAD_INFO_H */
diff --git a/arch/arm/kernel/entry-armv.S b/arch/arm/kernel/entry-armv.S
index 672b219..015e91a 100644
--- a/arch/arm/kernel/entry-armv.S
+++ b/arch/arm/kernel/entry-armv.S
@@ -1103,7 +1103,11 @@ vector_rst:
.long __irq_invalid @ c
.long __irq_invalid @ d
.long __irq_invalid @ e
+#ifdef CONFIG_KERNEL_MODE_LINUX
+ .long __irq_usr @ f
+#else
.long __irq_invalid @ f
+#endif
/*
* Data abort dispatcher
@@ -1126,7 +1130,11 @@ vector_rst:
.long __dabt_invalid @ c
.long __dabt_invalid @ d
.long __dabt_invalid @ e
+#ifdef CONFIG_KERNEL_MODE_LINUX
+ .long __dabt_usr @ f
+#else
.long __dabt_invalid @ f
+#endif
/*
* Prefetch abort dispatcher
@@ -1149,7 +1157,11 @@ vector_rst:
.long __pabt_invalid @ c
.long __pabt_invalid @ d
.long __pabt_invalid @ e
+#ifdef CONFIG_KERNEL_MODE_LINUX
+ .long __pabt_usr @ f
+#else
.long __pabt_invalid @ f
+#endif
/*
* Undef instr entry dispatcher
@@ -1172,7 +1184,11 @@ vector_rst:
.long __und_invalid @ c
.long __und_invalid @ d
.long __und_invalid @ e
+#ifdef CONFIG_KERNEL_MODE_LINUX
+ .long __und_usr @ f
+#else
.long __und_invalid @ f
+#endif
.align 5
diff --git a/arch/arm/lib/uaccess_with_memcpy.c b/arch/arm/lib/uaccess_with_memcpy.c
index 3e58d71..a399374 100644
--- a/arch/arm/lib/uaccess_with_memcpy.c
+++ b/arch/arm/lib/uaccess_with_memcpy.c
@@ -90,7 +90,11 @@ __copy_to_user_memcpy(void __user *to, const void *from, unsigned long n)
{
int atomic;
+#ifndef CONFIG_KERNEL_MODE_LINUX
if (unlikely(segment_eq(get_fs(), KERNEL_DS))) {
+#elseif
+ if (unlikely(segment_eq(get_fs(), KERNEL_DS) && !test_thread_flag(TIF_KU))) {
+#endif
memcpy((void *)to, from, n);
return 0;
}
@@ -153,7 +157,11 @@ __copy_to_user(void __user *to, const void *from, unsigned long n)
static unsigned long noinline
__clear_user_memset(void __user *addr, unsigned long n)
{
+#ifndef CONFIG_KERNEL_MODE_LINUX
if (unlikely(segment_eq(get_fs(), KERNEL_DS))) {
+#elseif
+ if (unlikely(segment_eq(get_fs(), KERNEL_DS) && !test_thread_flag(TIF_KU))) {
+#endif
memset((void *)addr, 0, n);
return 0;
}
diff --git a/arch/arm/vfp/vfphw.S b/arch/arm/vfp/vfphw.S
index f74a8f7..69cdb2f 100644
--- a/arch/arm/vfp/vfphw.S
+++ b/arch/arm/vfp/vfphw.S
@@ -84,8 +84,21 @@ ENTRY(vfp_support_entry)
ldr r3, [sp, #S_PSR] @ Neither lazy restore nor FP exceptions
and r3, r3, #MODE_MASK @ are supported in kernel mode
teq r3, #USR_MODE
+#ifndef CONFIG_KERNEL_MODE_LINUX
bne vfp_kmode_exception @ Returns through lr
+#elseif
+ beq vfp_user_mode
+ teq r3, #SYSTEM_MODE
+ bne vfp_kmode_exception
+ get_thread_info r3
+ ldr r3, [r3, #TI_FLAGS]
+ tst r3, #_TIF_KU
+ beq vfp_kmode_exception
+#endif
+#ifdef CONFIG_KERNEL_MODE_LINUX
+vfp_user_mode:
+#endif
VFPFMRX r1, FPEXC @ Is the VFP enabled?
DBGSTR1 "fpexc %08x", r1
tst r1, #FPEXC_EN
diff --git a/arch/microblaze/Kconfig b/arch/microblaze/Kconfig
index 0bce820..53866dc 100644
--- a/arch/microblaze/Kconfig
+++ b/arch/microblaze/Kconfig
@@ -284,6 +284,10 @@ endmenu
source "net/Kconfig"
+if (MMU)
+source "kernel/Kconfig.kml"
+endif
+
source "drivers/Kconfig"
source "fs/Kconfig"
diff --git a/arch/microblaze/include/asm/processor.h b/arch/microblaze/include/asm/processor.h
index 497a988..5a643b7 100644
--- a/arch/microblaze/include/asm/processor.h
+++ b/arch/microblaze/include/asm/processor.h
@@ -122,6 +122,11 @@ struct thread_struct {
.pgdir = swapper_pg_dir, \
}
+#ifdef CONFIG_KERNEL_MODE_LINUX
+void start_kernel_thread(struct pt_regs *regs,
+ unsigned long pc, unsigned long usp);
+#endif
+
/* Free all resources held by a thread. */
static inline void release_thread(struct task_struct *dead_task)
{
diff --git a/arch/microblaze/include/asm/thread_info.h b/arch/microblaze/include/asm/thread_info.h
index b699fbd..ddc9651 100644
--- a/arch/microblaze/include/asm/thread_info.h
+++ b/arch/microblaze/include/asm/thread_info.h
@@ -122,6 +122,10 @@ static inline struct thread_info *current_thread_info(void)
/* true if poll_idle() is polling TIF_NEED_RESCHED */
#define TIF_POLLING_NRFLAG 16
+#ifdef CONFIG_KERNEL_MODE_LINUX
+#define TIF_KU 31
+#endif
+
#define _TIF_SYSCALL_TRACE (1 << TIF_SYSCALL_TRACE)
#define _TIF_NOTIFY_RESUME (1 << TIF_NOTIFY_RESUME)
#define _TIF_SIGPENDING (1 << TIF_SIGPENDING)
@@ -131,6 +135,10 @@ static inline struct thread_info *current_thread_info(void)
#define _TIF_SYSCALL_AUDIT (1 << TIF_SYSCALL_AUDIT)
#define _TIF_SECCOMP (1 << TIF_SECCOMP)
+#ifdef CONFIG_KERNEL_MODE_LINUX
+#define _TIF_KU (1 << TIF_KU)
+#endif
+
/* work to do in syscall trace */
#define _TIF_WORK_SYSCALL_MASK (_TIF_SYSCALL_TRACE | _TIF_SINGLESTEP | \
_TIF_SYSCALL_AUDIT | _TIF_SECCOMP)
diff --git a/arch/microblaze/kernel/process.c b/arch/microblaze/kernel/process.c
index b2dd3719..c4221be 100644
--- a/arch/microblaze/kernel/process.c
+++ b/arch/microblaze/kernel/process.c
@@ -149,7 +149,22 @@ void start_thread(struct pt_regs *regs, unsigned long pc, unsigned long usp)
regs->msr |= MSR_UMS;
regs->msr &= ~MSR_VM;
#endif
+#ifdef CONFIG_KERNEL_MODE_LINUX
+ clear_thread_flag(TIF_KU);
+#endif
+}
+
+#ifdef CONFIG_KERNEL_MODE_LINUX
+void start_kernel_thread(struct pt_regs *regs, unsigned long pc, unsigned long usp)
+{
+ set_fs(KERNEL_DS);
+ regs->pc = pc;
+ regs->r1 = usp;
+ regs->pt_mode = 0;
+ regs->msr &= ~MSR_UMS;
+ set_thread_flag(TIF_KU);
}
+#endif
#ifdef CONFIG_MMU
#include <linux/elfcore.h>
diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig
index b7d31ca..5239012 100644
--- a/arch/x86/Kconfig
+++ b/arch/x86/Kconfig
@@ -2580,6 +2580,10 @@ config PMC_ATOM
source "net/Kconfig"
+if (X86_64 || X86_32) && !PARAVIRT && !CC_STACKPROTECTOR
+source "kernel/Kconfig.kml"
+endif
+
source "drivers/Kconfig"
source "drivers/firmware/Kconfig"
diff --git a/arch/x86/include/asm/cpufeature.h b/arch/x86/include/asm/cpufeature.h
index 90a5485..5aa31b8 100644
--- a/arch/x86/include/asm/cpufeature.h
+++ b/arch/x86/include/asm/cpufeature.h
@@ -390,6 +390,9 @@ extern const char * const x86_bug_flags[NBUGINTS*32];
#define cpu_has_eager_fpu boot_cpu_has(X86_FEATURE_EAGER_FPU)
#define cpu_has_topoext boot_cpu_has(X86_FEATURE_TOPOEXT)
#define cpu_has_bpext boot_cpu_has(X86_FEATURE_BPEXT)
+#ifdef CONFIG_KERNEL_MODE_LINUX
+#define cpu_has_smap boot_cpu_has(X86_FEATURE_SMAP)
+#endif
#if __GNUC__ >= 4
extern void warn_pre_alternatives(void);
diff --git a/arch/x86/include/asm/desc.h b/arch/x86/include/asm/desc.h
index a94b82e..4958340 100644
--- a/arch/x86/include/asm/desc.h
+++ b/arch/x86/include/asm/desc.h
@@ -98,7 +98,10 @@ static inline int desc_empty(const void *ptr)
#define store_idt(dtr) native_store_idt(dtr)
#define store_tr(tr) (tr = native_store_tr())
-#define load_TLS(t, cpu) native_load_tls(t, cpu)
+#define load_TLS__nmi_unsafe(t, cpu) native_load_tls__nmi_unsafe(t, cpu)
+#ifdef CONFIG_X86_64
+#define load_TLS(t, cpu) load_TLS__nmi_unsafe(t, cpu)
+#endif
#define set_ldt native_set_ldt
#define write_ldt_entry(dt, entry, desc) native_write_ldt_entry(dt, entry, desc)
@@ -112,25 +115,47 @@ static inline void paravirt_alloc_ldt(struct desc_struct *ldt, unsigned entries)
static inline void paravirt_free_ldt(struct desc_struct *ldt, unsigned entries)
{
}
-#endif /* CONFIG_PARAVIRT */
+#endif /* CONFIG_PARAVIRT */
#define store_ldt(ldt) asm("sldt %0" : "=m"(ldt))
static inline void native_write_idt_entry(gate_desc *idt, int entry, const gate_desc *gate)
{
+#ifdef CONFIG_KERNEL_MODE_LINUX
+ NMI_DECLS_GSLDTR
+ preempt_disable();
+ NMI_SAVE_GSLDTR;
+#endif
memcpy(&idt[entry], gate, sizeof(*gate));
+#ifdef CONFIG_KERNEL_MODE_LINUX
+ NMI_RESTORE_GSLDTR;
+ preempt_enable();
+#endif
}
static inline void native_write_ldt_entry(struct desc_struct *ldt, int entry, const void *desc)
{
+#ifdef CONFIG_KERNEL_MODE_LINUX
+ NMI_DECLS_GSLDTR
+ preempt_disable();
+ NMI_SAVE_GSLDTR;
+#endif
memcpy(&ldt[entry], desc, 8);
+#ifdef CONFIG_KERNEL_MODE_LINUX
+ NMI_RESTORE_GSLDTR;
+ preempt_enable();
+#endif
}
static inline void
native_write_gdt_entry(struct desc_struct *gdt, int entry, const void *desc, int type)
{
unsigned int size;
-
+#ifdef CONFIG_KERNEL_MODE_LINUX
+ NMI_DECLS_GSLDTR
+ preempt_disable();
+ NMI_SAVE_GSLDTR;
+#endif
switch (type) {
case DESC_TSS: size = sizeof(tss_desc); break;
case DESC_LDT: size = sizeof(ldt_desc); break;
@@ -138,6 +163,10 @@ native_write_gdt_entry(struct desc_struct *gdt, int entry, const void *desc, int
}
memcpy(&gdt[entry], desc, size);
+#ifdef CONFIG_KERNEL_MODE_LINUX
+ NMI_RESTORE_GSLDTR;
+ preempt_enable();
+#endif
}
static inline void pack_descriptor(struct desc_struct *desc, unsigned long base,
@@ -192,11 +221,24 @@ static inline void __set_tss_desc(unsigned cpu, unsigned int entry, void *addr)
#define set_tss_desc(cpu, addr) __set_tss_desc(cpu, GDT_ENTRY_TSS, addr)
+#ifdef CONFIG_KERNEL_MODE_LINUX
+
+static inline void clear_busy_flag_in_tss_descriptor(unsigned int cpu)
+{
+ get_cpu_gdt_table(cpu)[GDT_ENTRY_TSS].b &= (~0x00000200);
+}
+
+#endif
+
static inline void native_set_ldt(const void *addr, unsigned int entries)
{
- if (likely(entries == 0))
+ if (likely(entries == 0)) {
+#if defined(CONFIG_KERNEL_MODE_LINUX) && defined(CONFIG_X86_32)
+ unsigned cpu = smp_processor_id();
+ per_cpu(init_tss, cpu).x86_tss.ldt = 0;
+#endif
asm volatile("lldt %w0"::"q" (0));
- else {
+ } else {
unsigned cpu = smp_processor_id();
ldt_desc ldt;
@@ -204,6 +246,9 @@ static inline void native_set_ldt(const void *addr, unsigned int entries)
entries * LDT_ENTRY_SIZE - 1);
write_gdt_entry(get_cpu_gdt_table(cpu), GDT_ENTRY_LDT,
&ldt, DESC_LDT);
+#if defined(CONFIG_KERNEL_MODE_LINUX) && defined(CONFIG_X86_32)
+ per_cpu(init_tss, cpu).x86_tss.ldt = GDT_ENTRY_LDT * 8;
+#endif
asm volatile("lldt %w0"::"q" (GDT_ENTRY_LDT*8));
}
}
@@ -242,7 +287,7 @@ static inline unsigned long native_store_tr(void)
return tr;
}
-static inline void native_load_tls(struct thread_struct *t, unsigned int cpu)
+static inline void native_load_tls__nmi_unsafe(struct thread_struct *t, unsigned int cpu)
{
struct desc_struct *gdt = get_cpu_gdt_table(cpu);
unsigned int i;
@@ -376,14 +421,36 @@ static inline void _set_gate(int gate, unsigned type, void *addr,
* Pentium F0 0F bugfix can have resulted in the mapped
* IDT being write-protected.
*/
-#define set_intr_gate(n, addr) \
+#define priv_set_intr_gate_ist(n, addr, ist) \
do { \
BUG_ON((unsigned)n > 0xFF); \
- _set_gate(n, GATE_INTERRUPT, (void *)addr, 0, 0, \
+ _set_gate(n, GATE_INTERRUPT, (void *)addr, 0, ist, \
__KERNEL_CS); \
_trace_set_gate(n, GATE_INTERRUPT, (void *)trace_##addr,\
- 0, 0, __KERNEL_CS); \
+ 0, ist, __KERNEL_CS); \
+ } while (0)
+
+#if defined(CONFIG_KERNEL_MODE_LINUX) && defined(CONFIG_X86_64)
+
+#define set_intr_gate(n, addr) \
+ do { \
+ priv_set_intr_gate_ist(n, addr, KML_STACK); \
+ } while (0)
+
+#else
+
+#define set_intr_gate(n, addr) \
+ do { \
+ priv_set_intr_gate_ist(n, addr, 0); \
} while (0)
+#endif
+
+#ifdef CONFIG_KERNEL_MODE_LINUX
+#define set_intr_gate_orig(n, addr) \
+ do { \
+ priv_set_intr_gate_ist(n, addr, 0); \
+ } while (0)
+#endif
extern int first_system_vector;
/* used_vectors is BITMAP for irq is not managed by percpu vector_irq */
@@ -409,11 +476,22 @@ static inline void alloc_system_vector(int vector)
/*
* This routine sets up an interrupt gate at directory privilege level 3.
*/
+static void set_system_intr_gate_ist(int n, void *addr, unsigned ist);
static inline void set_system_intr_gate(unsigned int n, void *addr)
{
- BUG_ON((unsigned)n > 0xFF);
- _set_gate(n, GATE_INTERRUPT, addr, 0x3, 0, __KERNEL_CS);
+#if defined(CONFIG_KERNEL_MODE_LINUX) && defined(CONFIG_X86_64)
+ set_system_intr_gate_ist(n, addr, KML_STACK);
+#else
+ set_system_intr_gate_ist(n, addr, 0);
+#endif
+}
+
+#ifdef CONFIG_KERNEL_MODE_LINUX
+static inline void set_system_intr_gate_orig(unsigned int n, void *addr)
+{
+ set_system_intr_gate_ist(n, addr, 0);
}
+#endif
static inline void set_system_trap_gate(unsigned int n, void *addr)
{
diff --git a/arch/x86/include/asm/elf.h b/arch/x86/include/asm/elf.h
index ca3347a..c041ba5 100644
--- a/arch/x86/include/asm/elf.h
+++ b/arch/x86/include/asm/elf.h
@@ -275,7 +275,7 @@ extern int force_personality32;
struct task_struct;
-#define ARCH_DLINFO_IA32 \
+#define ARCH_DLINFO_IA32_ORIG \
do { \
if (vdso32_enabled) { \
NEW_AUX_ENT(AT_SYSINFO, VDSO_ENTRY); \
@@ -283,12 +283,33 @@ do { \
} \
} while (0)
+#define ARCH_DLINFO_IA32_KML \
+do { \
+ if (vdso32_enabled) { \
+ NEW_AUX_ENT(AT_SYSINFO, \
+ (kernel_mode ? VDSO_KML_ENTRY : VDSO_ENTRY));\
+ NEW_AUX_ENT(AT_SYSINFO_EHDR, VDSO_CURRENT_BASE); \
+ } \
+} while (0)
+
+#ifndef CONFIG_KERNEL_MODE_LINUX
+#define ARCH_DLINFO_IA32 ARCH_DLINFO_IA32_ORIG
+#else
+#define ARCH_DLINFO_IA32 ARCH_DLINFO_IA32_KML
+#endif
+
#ifdef CONFIG_X86_32
#define STACK_RND_MASK (0x7ff)
#define ARCH_DLINFO ARCH_DLINFO_IA32
+#ifdef CONFIG_KERNEL_MODE_LINUX
+#define VDSO_KML_ENTRY \
+ ((unsigned long)current->mm->context.vdso + \
+ selected_vdso32->sym___kernel_vsyscall_kml)
+#endif
+
/* update AT_VECTOR_SIZE_ARCH if the number of NEW_AUX_ENT entries changes */
#else /* CONFIG_X86_32 */
@@ -296,13 +317,37 @@ do { \
/* 1GB for 64bit, 8MB for 32bit */
#define STACK_RND_MASK (test_thread_flag(TIF_ADDR32) ? 0x7ff : 0x3fffff)
-#define ARCH_DLINFO \
+#define ARCH_DLINFO_ORIG \
do { \
if (vdso64_enabled) \
NEW_AUX_ENT(AT_SYSINFO_EHDR, \
(unsigned long __force)current->mm->context.vdso); \
} while (0)
+#ifdef CONFIG_KERNEL_MODE_LINUX
+extern void kml_call_entry(void);
+#endif
+
+#define VDSO_KML_ENTRY \
+ ((unsigned long) kml_call_entry)
+
+#define ARCH_DLINFO_KML \
+do { \
+ if (vdso64_enabled) { \
+ if (kernel_mode) { \
+ NEW_AUX_ENT(AT_SYSINFO, VDSO_KML_ENTRY); \
+ } \
+ NEW_AUX_ENT(AT_SYSINFO_EHDR, \
+ (unsigned long __force)current->mm->context.vdso); \
+ } \
+} while (0)
+
+#ifndef CONFIG_KERNEL_MODE_LINUX
+#define ARCH_DLINFO ARCH_DLINFO_ORIG
+#else
+#define ARCH_DLINFO ARCH_DLINFO_KML
+#endif
+
/* As a historical oddity, the x32 and x86_64 vDSOs are controlled together. */
#define ARCH_DLINFO_X32 \
do { \
@@ -317,7 +362,7 @@ do { \
if (test_thread_flag(TIF_X32)) \
ARCH_DLINFO_X32; \
else \
- ARCH_DLINFO_IA32
+ ARCH_DLINFO_IA32_ORIG
#define COMPAT_ELF_ET_DYN_BASE (TASK_UNMAPPED_BASE + 0x1000000)
diff --git a/arch/x86/include/asm/hw_irq.h b/arch/x86/include/asm/hw_irq.h
index 9662290..01ce1df 100644
--- a/arch/x86/include/asm/hw_irq.h
+++ b/arch/x86/include/asm/hw_irq.h
@@ -193,6 +193,37 @@ extern void (*__initconst interrupt[FIRST_SYSTEM_VECTOR
typedef int vector_irq_t[NR_VECTORS];
DECLARE_PER_CPU(vector_irq_t, vector_irq);
+#if defined(CONFIG_KERNEL_MODE_LINUX) && defined(CONFIG_X86_32)
+extern struct desc_struct idt_table[256];
+extern void (*test_ISR_and_handle_interrupt)(void);
+
+static inline unsigned long get_address_from_desc(struct desc_struct* s)
+{
+ return (s->a & 0x0000ffff) | (s->b & 0xffff0000);
+}
+
+static inline unsigned long get_intr_address(unsigned long vec)
+{
+ return get_address_from_desc(&idt_table[vec]);
+}
+
+static inline void handle_interrupt_manually(unsigned long vec)
+{
+ unsigned long handler;
+
+ handler = get_intr_address(vec);
+
+ __asm__ __volatile__ (
+ "pushfl\n\t"
+ "pushl %1\n\t"
+ "pushl $0f\n\t"
+ "jmp *%0\n"
+ "0:\n\t"
+ : : "r" (handler), "i" (__KERNEL_CS)
+ );
+}
+#endif
+
#endif /* !ASSEMBLY_ */
#endif /* _ASM_X86_HW_IRQ_H */
diff --git a/arch/x86/include/asm/page_64_types.h b/arch/x86/include/asm/page_64_types.h
index 4edd53b..7c1301f 100644
--- a/arch/x86/include/asm/page_64_types.h
+++ b/arch/x86/include/asm/page_64_types.h
@@ -24,7 +24,12 @@
#define NMI_STACK 2
#define DEBUG_STACK 3
#define MCE_STACK 4
+#ifndef CONFIG_KERNEL_MODE_LINUX
#define N_EXCEPTION_STACKS 4 /* hw limit: 7 */
+#else
+#define KML_STACK 5
+#define N_EXCEPTION_STACKS 5 /* hw limit: 7 */
+#endif
#define PUD_PAGE_SIZE (_AC(1, UL) << PUD_SHIFT)
#define PUD_PAGE_MASK (~(PUD_PAGE_SIZE-1))
diff --git a/arch/x86/include/asm/processor.h b/arch/x86/include/asm/processor.h
index ec1c935..9ef442d 100644
--- a/arch/x86/include/asm/processor.h
+++ b/arch/x86/include/asm/processor.h
@@ -146,7 +146,9 @@ struct cpuinfo_x86 {
extern struct cpuinfo_x86 boot_cpu_data;
extern struct cpuinfo_x86 new_cpu_data;
+#ifndef CONFIG_KERNEL_MODE_LINUX
extern struct tss_struct doublefault_tss;
+#endif
extern __u32 cpu_caps_cleared[NCAPINTS];
extern __u32 cpu_caps_set[NCAPINTS];
@@ -198,11 +200,6 @@ static inline void native_cpuid(unsigned int *eax, unsigned int *ebx,
: "memory");
}
-static inline void load_cr3(pgd_t *pgdir)
-{
- write_cr3(__pa(pgdir));
-}
-
#ifdef CONFIG_X86_32
/* This is the TSS defined by the hardware. */
struct x86_hw_tss {
@@ -280,10 +277,25 @@ struct tss_struct {
*/
unsigned long stack[64];
+#ifdef CONFIG_KERNEL_MODE_LINUX
+#define KML_STACK_SIZE (8*16)
+ char kml_stack[KML_STACK_SIZE] __attribute__ ((aligned (16)));
+#endif
} ____cacheline_aligned;
DECLARE_PER_CPU_SHARED_ALIGNED(struct tss_struct, init_tss);
+#ifdef CONFIG_KERNEL_MODE_LINUX
+DECLARE_PER_CPU(struct tss_struct, doublefault_tsses);
+DECLARE_PER_CPU(struct tss_struct, nmi_tsses);
+DECLARE_PER_CPU(struct dft_stack_struct, dft_stacks);
+DECLARE_PER_CPU(struct nmi_stack_struct, nmi_stacks);
+DECLARE_PER_CPU(unsigned long, esp0);
+DECLARE_PER_CPU(unsigned long, unused);
+extern void init_doublefault_tss(int);
+extern void init_nmi_tss(int);
+#endif
+
/*
* Save the original ist values for checking stack pointers during debugging
*/
@@ -525,6 +537,23 @@ struct thread_struct {
unsigned char fpu_counter;
};
+#ifdef CONFIG_KERNEL_MODE_LINUX
+struct dft_stack_struct {