-
Notifications
You must be signed in to change notification settings - Fork 2
/
0001-patch-v2.patch
1849 lines (1777 loc) · 52.6 KB
/
0001-patch-v2.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
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
From 02f6467c5da02a0d16805ad6fe506a2a3d68fbd7 Mon Sep 17 00:00:00 2001
From: fengeh <2748250768@qq.com>
Date: Thu, 19 Aug 2021 17:25:21 +0800
Subject: [PATCH] patch-v2
---
arch/riscv/Kconfig | 14 ++
arch/riscv/configs/defconfig | 4 +-
arch/riscv/include/asm/pgalloc.h | 32 ++++
arch/riscv/include/asm/pgtable-64.h | 21 +++
arch/riscv/include/asm/pgtable.h | 81 +++++++-
arch/riscv/include/asm/sbi.h | 10 +
arch/riscv/kernel/head.S | 7 +-
arch/riscv/kernel/sbi.c | 61 ++++++
arch/riscv/kernel/setup.c | 5 +-
arch/riscv/kernel/time.c | 11 +-
arch/riscv/mm/fault.c | 1 +
arch/riscv/mm/init.c | 5 +-
fs/stat.c | 1 +
include/asm-generic/pgalloc.h | 150 +++++++++++++++
include/linux/mm.h | 4 +
include/linux/mmzone.h | 2 +-
include/linux/pgtable.h | 42 +++++
include/linux/pt_area.h | 25 +++
init/main.c | 86 +++++++++
mm/Makefile | 4 +
mm/filemap.c | 21 ++-
mm/memory.c | 254 ++++++++++++++++++++++++-
mm/oom_kill.c | 16 +-
mm/pt_area.c | 283 ++++++++++++++++++++++++++++
mm/swap_state.c | 17 +-
mm/vmalloc.c | 95 +++++++++-
26 files changed, 1227 insertions(+), 25 deletions(-)
create mode 100644 include/linux/pt_area.h
create mode 100644 mm/pt_area.c
diff --git a/arch/riscv/Kconfig b/arch/riscv/Kconfig
index 44377fd78..b0f2d66e5 100644
--- a/arch/riscv/Kconfig
+++ b/arch/riscv/Kconfig
@@ -111,6 +111,20 @@ config RISCV_SBI
depends on !RISCV_M_MODE
default y
+# set if we enable PT_AREA for penglai enclave
+config PT_AREA
+ bool
+ default y
+
+config PT_AREA_BATCH
+ bool
+ depends on PT_AREA
+ default y
+
+config EARLY_PRINTK
+ bool
+ default y
+
config MMU
bool "MMU-based Paged Memory Management Support"
default y
diff --git a/arch/riscv/configs/defconfig b/arch/riscv/configs/defconfig
index d222d353d..5c641aeb2 100644
--- a/arch/riscv/configs/defconfig
+++ b/arch/riscv/configs/defconfig
@@ -118,7 +118,7 @@ CONFIG_DEBUG_RT_MUTEXES=y
CONFIG_DEBUG_SPINLOCK=y
CONFIG_DEBUG_MUTEXES=y
CONFIG_DEBUG_RWSEMS=y
-CONFIG_DEBUG_ATOMIC_SLEEP=y
+CONFIG_DEBUG_ATOMIC_SLEEP=n
CONFIG_STACKTRACE=y
CONFIG_DEBUG_LIST=y
CONFIG_DEBUG_PLIST=y
@@ -131,3 +131,5 @@ CONFIG_DEBUG_BLOCK_EXT_DEVT=y
CONFIG_MEMTEST=y
# CONFIG_SYSFS_SYSCALL is not set
CONFIG_EFI=y
+CONFIG_CMDLINE="earlycon=ttyS0 console=hvc"
+CONFIG_CMDLINE_FORCE=y
diff --git a/arch/riscv/include/asm/pgalloc.h b/arch/riscv/include/asm/pgalloc.h
index 23b1544e0..4653ad5e8 100644
--- a/arch/riscv/include/asm/pgalloc.h
+++ b/arch/riscv/include/asm/pgalloc.h
@@ -10,6 +10,11 @@
#include <linux/mm.h>
#include <asm/tlb.h>
+#ifdef CONFIG_PT_AREA
+#include <linux/pt_area.h>
+#include <asm/sbi.h>
+#endif
+
#ifdef CONFIG_MMU
#include <asm-generic/pgalloc.h>
@@ -39,7 +44,33 @@ static inline void pud_populate(struct mm_struct *mm, pud_t *pud, pmd_t *pmd)
#endif /* __PAGETABLE_PMD_FOLDED */
#define pmd_pgtable(pmd) pmd_page(pmd)
+#ifdef CONFIG_PT_AREA
+static inline pgd_t *pgd_alloc(struct mm_struct *mm)
+{
+ pgd_t *pgd;
+ pgd = (pgd_t *)alloc_pt_pgd_page();
+ if(likely(pgd != NULL))
+ {
+ if(enclave_module_installed)
+ {
+ SBI_PENGLAI_ECALL_4(SBI_SM_SET_PTE, SBI_PTE_MEMSET, __pa(pgd), 0, USER_PTRS_PER_PGD * sizeof(pgd_t));
+ SBI_PENGLAI_ECALL_4(SBI_SM_SET_PTE, SBI_PTE_MEMCPY, __pa(pgd + USER_PTRS_PER_PGD),
+ __pa(init_mm.pgd + USER_PTRS_PER_PGD),
+ (PTRS_PER_PGD - USER_PTRS_PER_PGD) * sizeof(pgd_t));
+ }
+ else
+ {
+ memset(pgd, 0, USER_PTRS_PER_PGD * sizeof(pgd_t));
+ /* Copy kernel mappings */
+ memcpy(pgd + USER_PTRS_PER_PGD,
+ init_mm.pgd + USER_PTRS_PER_PGD,
+ (PTRS_PER_PGD - USER_PTRS_PER_PGD) * sizeof(pgd_t));
+ }
+ }
+ return pgd;
+}
+#else /* CONFIG_PT_AREA */
static inline pgd_t *pgd_alloc(struct mm_struct *mm)
{
pgd_t *pgd;
@@ -54,6 +85,7 @@ static inline pgd_t *pgd_alloc(struct mm_struct *mm)
}
return pgd;
}
+#endif /* CONFIG_PT_AREA */
#ifndef __PAGETABLE_PMD_FOLDED
diff --git a/arch/riscv/include/asm/pgtable-64.h b/arch/riscv/include/asm/pgtable-64.h
index f3b0da64c..e7129dd69 100644
--- a/arch/riscv/include/asm/pgtable-64.h
+++ b/arch/riscv/include/asm/pgtable-64.h
@@ -7,6 +7,7 @@
#define _ASM_RISCV_PGTABLE_64_H
#include <linux/const.h>
+#include <asm/sbi.h>
#define PGDIR_SHIFT 30
/* Size of region mapped by a page global directory */
@@ -50,10 +51,30 @@ static inline int pud_leaf(pud_t pud)
(pud_val(pud) & (_PAGE_READ | _PAGE_WRITE | _PAGE_EXEC));
}
+#ifdef CONFIG_PT_AREA
+extern int enclave_module_installed;
+#define SBI_SM_SET_PTE 101
+#define SBI_SET_PTE_ONE 1
+#define SBI_PTE_MEMSET 2
+#define SBI_PTE_MEMCPY 3
+
+static void set_pud(pud_t *pudp, pud_t pud)
+{
+ if(enclave_module_installed)
+ {
+ SBI_PENGLAI_ECALL_4(SBI_SM_SET_PTE, SBI_SET_PTE_ONE, __pa(pudp), pud.p4d.pgd.pgd, 0);
+ }
+ else
+ {
+ *pudp = pud;
+ }
+}
+#else
static inline void set_pud(pud_t *pudp, pud_t pud)
{
*pudp = pud;
}
+#endif /*CONFIG_PT_AREA*/
static inline void pud_clear(pud_t *pudp)
{
diff --git a/arch/riscv/include/asm/pgtable.h b/arch/riscv/include/asm/pgtable.h
index 183f1f4b2..de5aed65a 100644
--- a/arch/riscv/include/asm/pgtable.h
+++ b/arch/riscv/include/asm/pgtable.h
@@ -18,6 +18,8 @@
#include <asm/page.h>
#include <asm/tlbflush.h>
#include <linux/mm_types.h>
+#include <asm/sbi.h>
+#include <linux/kernel.h>
#ifdef CONFIG_MMU
@@ -114,6 +116,10 @@
#define _PAGE_IOREMAP _PAGE_KERNEL
extern pgd_t swapper_pg_dir[];
+//penglai extension
+#ifdef CONFIG_PT_AREA
+extern pgd_t *new_swapper_pg_dir;
+#endif /*CONFIG_PT_AREA*/
/* MAP_PRIVATE permissions: xwr (copy-on-write) */
#define __P000 PAGE_NONE
@@ -159,7 +165,16 @@ static inline int pmd_leaf(pmd_t pmd)
static inline void set_pmd(pmd_t *pmdp, pmd_t pmd)
{
+ #ifdef CONFIG_PT_AREA
+ if(enclave_module_installed)
+ {
+ SBI_PENGLAI_ECALL_4(SBI_SM_SET_PTE, SBI_SET_PTE_ONE, __pa(pmdp), pmd.pmd, 0);
+ }
+ else
+ *pmdp=pmd;
+ #else
*pmdp = pmd;
+ #endif /*CONFIG_PT_AREA*/
}
static inline void pmd_clear(pmd_t *pmdp)
@@ -320,6 +335,7 @@ static inline int pte_same(pte_t pte_a, pte_t pte_b)
return pte_val(pte_a) == pte_val(pte_b);
}
+
/*
* Certain architectures need to do special things when PTEs within
* a page table are directly modified. Thus, the following hook is
@@ -327,7 +343,19 @@ static inline int pte_same(pte_t pte_a, pte_t pte_b)
*/
static inline void set_pte(pte_t *ptep, pte_t pteval)
{
- *ptep = pteval;
+ #ifdef CONFIG_PT_AREA
+
+ if(enclave_module_installed)
+ {
+ // printk("set_pte\n");
+ // dump_stack();
+ SBI_PENGLAI_ECALL_4(SBI_SM_SET_PTE, SBI_SET_PTE_ONE, __pa(ptep), pteval.pte, 0);
+ }
+ else
+ *ptep = pteval;
+ #else
+ *ptep = pteval;
+ #endif /*CONFIG_PT_AREA*/
}
void flush_icache_pte(pte_t pte);
@@ -341,12 +369,33 @@ static inline void set_pte_at(struct mm_struct *mm,
set_pte(ptep, pteval);
}
+#ifdef CONFIG_PT_AREA_BATCH
+static inline void delay_set_pte_at(struct mm_struct *mm,
+ unsigned long addr, pte_t *ptep, pte_t pteval)
+{
+ if (pte_present(pteval) && pte_exec(pteval))
+ flush_icache_pte(pteval);
+
+ // set_pte(ptep, pteval);
+}
+#endif
+
static inline void pte_clear(struct mm_struct *mm,
unsigned long addr, pte_t *ptep)
{
set_pte_at(mm, addr, ptep, __pte(0));
}
+// delay pte set operation
+#ifdef CONFIG_PT_AREA_BATCH
+static inline void delay_pte_clear(struct mm_struct *mm,
+ unsigned long addr, pte_t *ptep)
+{
+ if (pte_present(__pte(0)) && pte_exec(__pte(0)))
+ flush_icache_pte(__pte(0));
+}
+#endif
+
#define __HAVE_ARCH_PTEP_SET_ACCESS_FLAGS
static inline int ptep_set_access_flags(struct vm_area_struct *vma,
unsigned long address, pte_t *ptep,
@@ -365,7 +414,18 @@ static inline int ptep_set_access_flags(struct vm_area_struct *vma,
static inline pte_t ptep_get_and_clear(struct mm_struct *mm,
unsigned long address, pte_t *ptep)
{
- return __pte(atomic_long_xchg((atomic_long_t *)ptep, 0));
+ #ifdef CONFIG_PT_AREA
+ if(enclave_module_installed)
+ {
+ pte_t pte = *ptep;
+ pte_clear(mm, address, ptep);
+ return pte;
+ }
+ else
+ return __pte(atomic_long_xchg((atomic_long_t *)ptep, 0));
+ #else
+ return __pte(atomic_long_xchg((atomic_long_t *)ptep, 0));
+ #endif
}
#define __HAVE_ARCH_PTEP_TEST_AND_CLEAR_YOUNG
@@ -382,7 +442,22 @@ static inline int ptep_test_and_clear_young(struct vm_area_struct *vma,
static inline void ptep_set_wrprotect(struct mm_struct *mm,
unsigned long address, pte_t *ptep)
{
- atomic_long_and(~(unsigned long)_PAGE_WRITE, (atomic_long_t *)ptep);
+ #ifdef CONFIG_PT_AREA
+ if(enclave_module_installed)
+ {
+ pte_t pteval;
+ pteval.pte = (~(unsigned long)_PAGE_WRITE) & (ptep->pte);
+ // printk("ptep_set_wrprotect\n");
+ // dump_stack();
+ SBI_PENGLAI_ECALL_4(SBI_SM_SET_PTE, SBI_SET_PTE_ONE, __pa(ptep), pteval.pte, 0);
+ }
+ else
+ {
+ atomic_long_and(~(unsigned long)_PAGE_WRITE, (atomic_long_t *)ptep);
+ }
+ #else
+ atomic_long_and(~(unsigned long)_PAGE_WRITE, (atomic_long_t *)ptep);
+ #endif
}
#define __HAVE_ARCH_PTEP_CLEAR_YOUNG_FLUSH
diff --git a/arch/riscv/include/asm/sbi.h b/arch/riscv/include/asm/sbi.h
index 653edb25d..56ac0c7db 100644
--- a/arch/riscv/include/asm/sbi.h
+++ b/arch/riscv/include/asm/sbi.h
@@ -27,6 +27,9 @@ enum sbi_ext_id {
SBI_EXT_IPI = 0x735049,
SBI_EXT_RFENCE = 0x52464E43,
SBI_EXT_HSM = 0x48534D,
+ //TODO:
+ //Why this magic number
+ SBI_EXT_PENGLAI = 0x100100,
};
enum sbi_ext_base_fid {
@@ -95,6 +98,13 @@ struct sbiret sbi_ecall(int ext, int fid, unsigned long arg0,
unsigned long arg3, unsigned long arg4,
unsigned long arg5);
+long SBI_PENGLAI_ECALL_0(int fid);
+long SBI_PENGLAI_ECALL_1(int fid, unsigned long arg0);
+long SBI_PENGLAI_ECALL_2(int fid, unsigned long arg0, unsigned long arg1);
+long SBI_PENGLAI_ECALL_3(int fid, unsigned long arg0, unsigned long arg1, unsigned long arg2);
+long SBI_PENGLAI_ECALL_4(int fid, unsigned long arg0, unsigned long arg1, unsigned long arg2, unsigned long arg3);
+long SBI_PENGLAI_ECALL_5(int fid, unsigned long arg0, unsigned long arg1, unsigned long arg2, unsigned long arg3, unsigned long arg4);
+
void sbi_console_putchar(int ch);
int sbi_console_getchar(void);
void sbi_set_timer(uint64_t stime_value);
diff --git a/arch/riscv/kernel/head.S b/arch/riscv/kernel/head.S
index 7e849797c..dab0f3c3a 100644
--- a/arch/riscv/kernel/head.S
+++ b/arch/riscv/kernel/head.S
@@ -155,7 +155,12 @@ secondary_start_common:
#ifdef CONFIG_MMU
/* Enable virtual memory and relocate to virtual address */
- la a0, swapper_pg_dir
+ #ifdef CONFIG_PT_AREA
+ la a0, new_swapper_pg_dir
+ ld a0, (a0)
+ #else
+ la a0, swapper_pg_dir
+ #endif
call relocate
#endif
call setup_trap_vector
diff --git a/arch/riscv/kernel/sbi.c b/arch/riscv/kernel/sbi.c
index 226ccce0f..98ce3c655 100644
--- a/arch/riscv/kernel/sbi.c
+++ b/arch/riscv/kernel/sbi.c
@@ -46,6 +46,67 @@ struct sbiret sbi_ecall(int ext, int fid, unsigned long arg0,
}
EXPORT_SYMBOL(sbi_ecall);
+long SBI_PENGLAI_ECALL_0(int fid)
+{
+ struct sbiret ret;
+ ret = sbi_ecall(SBI_EXT_PENGLAI, fid, 0, 0, 0, 0, 0, 0);
+ if (ret.error == 0)
+ return ret.value;
+ return ret.error;
+}
+
+long SBI_PENGLAI_ECALL_1(int fid, unsigned long arg0)
+{
+ struct sbiret ret;
+ ret = sbi_ecall(SBI_EXT_PENGLAI, fid, arg0, 0, 0, 0, 0, 0);
+ if (ret.error == 0)
+ return ret.value;
+ return ret.error;
+}
+
+long SBI_PENGLAI_ECALL_2(int fid, unsigned long arg0, unsigned long arg1)
+{
+ struct sbiret ret;
+ ret = sbi_ecall(SBI_EXT_PENGLAI, fid, arg0, arg1, 0, 0, 0, 0);
+ if (ret.error == 0)
+ return ret.value;
+ return ret.error;
+}
+
+long SBI_PENGLAI_ECALL_3(int fid, unsigned long arg0, unsigned long arg1, unsigned long arg2)
+{
+ struct sbiret ret;
+ ret = sbi_ecall(SBI_EXT_PENGLAI, fid, arg0, arg1, arg2, 0, 0, 0);
+ if (ret.error == 0)
+ return ret.value;
+ return ret.error;
+}
+
+long SBI_PENGLAI_ECALL_4(int fid, unsigned long arg0, unsigned long arg1, unsigned long arg2, unsigned long arg3)
+{
+ struct sbiret ret;
+ ret = sbi_ecall(SBI_EXT_PENGLAI, fid, arg0, arg1, arg2, arg3, 0, 0);
+ if (ret.error == 0)
+ return ret.value;
+ return ret.error;
+}
+
+long SBI_PENGLAI_ECALL_5(int fid, unsigned long arg0, unsigned long arg1, unsigned long arg2, unsigned long arg3, unsigned long arg4)
+{
+ struct sbiret ret;
+ ret = sbi_ecall(SBI_EXT_PENGLAI, fid, arg0, arg1, arg2, arg3, arg4, 0);
+ if (ret.error == 0)
+ return ret.value;
+ return ret.error;
+}
+
+EXPORT_SYMBOL(SBI_PENGLAI_ECALL_0);
+EXPORT_SYMBOL(SBI_PENGLAI_ECALL_1);
+EXPORT_SYMBOL(SBI_PENGLAI_ECALL_2);
+EXPORT_SYMBOL(SBI_PENGLAI_ECALL_3);
+EXPORT_SYMBOL(SBI_PENGLAI_ECALL_4);
+EXPORT_SYMBOL(SBI_PENGLAI_ECALL_5);
+
int sbi_err_map_linux_errno(int err)
{
switch (err) {
diff --git a/arch/riscv/kernel/setup.c b/arch/riscv/kernel/setup.c
index 117f3212a..e7ef5ca0b 100644
--- a/arch/riscv/kernel/setup.c
+++ b/arch/riscv/kernel/setup.c
@@ -54,9 +54,12 @@ static DEFINE_PER_CPU(struct cpu, cpu_devices);
static void __init parse_dtb(void)
{
/* Early scan of device tree from init memory */
+ strlcpy(boot_command_line, CONFIG_CMDLINE, COMMAND_LINE_SIZE);
if (early_init_dt_scan(dtb_early_va))
+ {
+ strlcpy(boot_command_line, CONFIG_CMDLINE, COMMAND_LINE_SIZE);
return;
-
+ }
pr_err("No DTB passed to the kernel\n");
#ifdef CONFIG_CMDLINE_FORCE
strlcpy(boot_command_line, CONFIG_CMDLINE, COMMAND_LINE_SIZE);
diff --git a/arch/riscv/kernel/time.c b/arch/riscv/kernel/time.c
index 4d3a1048a..0a76e5bf8 100644
--- a/arch/riscv/kernel/time.c
+++ b/arch/riscv/kernel/time.c
@@ -17,11 +17,12 @@ void __init time_init(void)
struct device_node *cpu;
u32 prop;
- cpu = of_find_node_by_path("/cpus");
- if (!cpu || of_property_read_u32(cpu, "timebase-frequency", &prop))
- panic(KERN_WARNING "RISC-V system with no 'timebase-frequency' in DTS\n");
- of_node_put(cpu);
- riscv_timebase = prop;
+ // cpu = of_find_node_by_path("/cpus");
+ // if (!cpu || of_property_read_u32(cpu, "timebase-frequency", &prop))
+ // panic(KERN_WARNING "RISC-V system with no 'timebase-frequency' in DTS\n");
+ // of_node_put(cpu);
+ // riscv_timebase = prop;
+ riscv_timebase = 1000000;
lpj_fine = riscv_timebase / HZ;
timer_probe();
diff --git a/arch/riscv/mm/fault.c b/arch/riscv/mm/fault.c
index 3c8b9e433..e5278cf2a 100644
--- a/arch/riscv/mm/fault.c
+++ b/arch/riscv/mm/fault.c
@@ -33,6 +33,7 @@ static inline void no_context(struct pt_regs *regs, unsigned long addr)
pr_alert("Unable to handle kernel %s at virtual address " REG_FMT "\n",
(addr < PAGE_SIZE) ? "NULL pointer dereference" :
"paging request", addr);
+ dump_stack();
die(regs, "Oops");
do_exit(SIGKILL);
}
diff --git a/arch/riscv/mm/init.c b/arch/riscv/mm/init.c
index 8e577f14f..43cc17989 100644
--- a/arch/riscv/mm/init.c
+++ b/arch/riscv/mm/init.c
@@ -208,9 +208,12 @@ EXPORT_SYMBOL(pfn_base);
pgd_t swapper_pg_dir[PTRS_PER_PGD] __page_aligned_bss;
pgd_t trampoline_pg_dir[PTRS_PER_PGD] __page_aligned_bss;
+#ifdef CONFIG_PT_AREA
+pgd_t *new_swapper_pg_dir;
+#endif
pte_t fixmap_pte[PTRS_PER_PTE] __page_aligned_bss;
-#define MAX_EARLY_MAPPING_SIZE SZ_128M
+#define MAX_EARLY_MAPPING_SIZE SZ_256M
pgd_t early_pg_dir[PTRS_PER_PGD] __initdata __aligned(PAGE_SIZE);
diff --git a/fs/stat.c b/fs/stat.c
index dacecdda2..8fdd475b1 100644
--- a/fs/stat.c
+++ b/fs/stat.c
@@ -207,6 +207,7 @@ int vfs_fstatat(int dfd, const char __user *filename,
return vfs_statx(dfd, filename, flags | AT_NO_AUTOMOUNT,
stat, STATX_BASIC_STATS);
}
+EXPORT_SYMBOL(vfs_fstatat);
#ifdef __ARCH_WANT_OLD_STAT
diff --git a/include/asm-generic/pgalloc.h b/include/asm-generic/pgalloc.h
index 02932efad..f53fded62 100644
--- a/include/asm-generic/pgalloc.h
+++ b/include/asm-generic/pgalloc.h
@@ -2,11 +2,159 @@
#ifndef __ASM_GENERIC_PGALLOC_H
#define __ASM_GENERIC_PGALLOC_H
+#ifdef CONFIG_PT_AREA
+#include <linux/pt_area.h>
+#include <asm/atomic.h>
+#endif
+
#ifdef CONFIG_MMU
#define GFP_PGTABLE_KERNEL (GFP_KERNEL | __GFP_ZERO)
#define GFP_PGTABLE_USER (GFP_PGTABLE_KERNEL | __GFP_ACCOUNT)
+#ifdef CONFIG_PT_AREA
+
+static inline pte_t *__pte_alloc_one_kernel(struct mm_struct *mm)
+{
+ pte_t* pte;
+ pte = (pte_t*) alloc_pt_pte_page();
+ return pte;
+}
+
+#ifndef __HAVE_ARCH_PTE_ALLOC_ONE_KERNEL
+static inline pte_t *pte_alloc_one_kernel(struct mm_struct *mm)
+{
+ return __pte_alloc_one_kernel(mm);
+}
+#endif
+
+
+
+
+static inline void pte_free_kernel(struct mm_struct *mm, pte_t *pte)
+{
+ free_pt_pte_page((unsigned long)pte);
+}
+
+static inline pgtable_t __pte_alloc_one(struct mm_struct *mm, gfp_t gfp)
+{
+ struct page *pte;
+ char *pt_pte_addr = alloc_pt_pte_page();
+ if (pt_pte_addr == NULL)
+ return NULL;
+
+ pte = virt_to_page((void*)(pt_pte_addr));
+ if (!pte)
+ return NULL;
+ if (!pgtable_pte_page_ctor(pte)) {
+ free_pt_pte_page((unsigned long)(page_address(pte)));
+ return NULL;
+ }
+ return pte;
+}
+
+#ifndef __HAVE_ARCH_PTE_ALLOC_ONE
+static inline pgtable_t pte_alloc_one(struct mm_struct *mm)
+{
+ return __pte_alloc_one(mm, GFP_PGTABLE_USER);
+}
+#endif
+
+static inline void pte_free(struct mm_struct *mm, struct page *pte_page)
+{
+ pgtable_pte_page_dtor(pte_page);
+ *(unsigned long *)&pte_page->ptl = 0;
+ free_pt_pte_page((unsigned long)(page_address(pte_page)));
+}
+
+static inline void pte_free_no_dtor(struct mm_struct *mm, struct page *pte_page)
+{
+ *(unsigned long *)&pte_page->ptl = 0;
+ free_pt_pte_page((unsigned long)(page_address(pte_page)));
+}
+
+static inline int check_pte(struct mm_struct *mm, struct page *pte_page)
+{
+ return check_pt_pte_page((unsigned long)(page_address(pte_page)));
+}
+
+#if CONFIG_PGTABLE_LEVELS > 2
+
+#ifndef __HAVE_ARCH_PMD_ALLOC_ONE
+static inline pmd_t *pmd_alloc_one(struct mm_struct *mm, unsigned long addr)
+{
+ struct page *page;
+ gfp_t gfp = GFP_PGTABLE_USER;
+
+ if (mm == &init_mm)
+ gfp = GFP_PGTABLE_KERNEL;
+
+ char *pt_pmd_addr = alloc_pt_pmd_page();
+ if (pt_pmd_addr == NULL)
+ return NULL;
+
+ page = virt_to_page((void *)pt_pmd_addr);
+ if (!page)
+ return NULL;
+ if (!pgtable_pmd_page_ctor(page)) {
+ free_pt_pmd_page((unsigned long)(page_address(page)));
+ return NULL;
+ }
+ return (pmd_t *)page_address(page);
+}
+#endif
+
+#ifndef __HAVE_ARCH_PMD_FREE
+static inline void pmd_free(struct mm_struct *mm, pmd_t *pmd)
+{
+ struct page *page = virt_to_page(pmd);
+ pgtable_pmd_page_dtor(page);
+ *(unsigned long *)&page->ptl = 0;
+ free_pt_pmd_page((unsigned long)pmd);
+}
+#endif
+
+#ifndef __HAVE_ARCH_PMD_FREE
+static inline void pmd_free_no_dtor(struct mm_struct *mm, pmd_t *pmd)
+{
+ free_pt_pmd_page((unsigned long)pmd);
+}
+#endif
+
+#endif /* CONFIG_PGTABLE_LEVELS > 2 */
+
+#if CONFIG_PGTABLE_LEVELS > 3
+
+#ifndef __HAVE_ARCH_PUD_ALLOC_ONE
+static inline pud_t *pud_alloc_one(struct mm_struct *mm, unsigned long addr)
+{
+ gfp_t gfp = GFP_PGTABLE_USER;
+
+ if (mm == &init_mm)
+ gfp = GFP_PGTABLE_KERNEL;
+ return (pud_t *)get_zeroed_page(gfp);
+}
+#endif
+
+static inline void pud_free(struct mm_struct *mm, pud_t *pud)
+{
+ BUG_ON((unsigned long)pud & (PAGE_SIZE-1));
+ free_page((unsigned long)pud);
+}
+
+#endif /* CONFIG_PGTABLE_LEVELS > 3 */
+
+#ifndef __HAVE_ARCH_PGD_FREE
+static inline void pgd_free(struct mm_struct *mm, pgd_t *pgd)
+{
+ free_pt_pgd_page((unsigned long)pgd);
+}
+#endif
+
+
+
+#else
+
/**
* __pte_alloc_one_kernel - allocate a page for PTE-level kernel page table
* @mm: the mm_struct of the current context
@@ -182,6 +330,8 @@ static inline void pgd_free(struct mm_struct *mm, pgd_t *pgd)
}
#endif
+#endif /* CONFIG_PT_AREA */
+
#endif /* CONFIG_MMU */
#endif /* __ASM_GENERIC_PGALLOC_H */
diff --git a/include/linux/mm.h b/include/linux/mm.h
index db6ae4d3f..5b6bdba0e 100644
--- a/include/linux/mm.h
+++ b/include/linux/mm.h
@@ -972,6 +972,10 @@ static inline pte_t maybe_mkwrite(pte_t pte, struct vm_area_struct *vma)
return pte;
}
+#ifdef CONFIG_PT_AREA_BATCH
+void flush_pt_area_set_buffer(void);
+vm_fault_t alloc_noset_pte(struct vm_fault *vmf, struct page *page);
+#endif
vm_fault_t alloc_set_pte(struct vm_fault *vmf, struct page *page);
vm_fault_t finish_fault(struct vm_fault *vmf);
vm_fault_t finish_mkwrite_fault(struct vm_fault *vmf);
diff --git a/include/linux/mmzone.h b/include/linux/mmzone.h
index fb3bf696c..6e2a1f8d7 100644
--- a/include/linux/mmzone.h
+++ b/include/linux/mmzone.h
@@ -24,7 +24,7 @@
/* Free memory management - zoned buddy allocator. */
#ifndef CONFIG_FORCE_MAX_ZONEORDER
-#define MAX_ORDER 11
+#define MAX_ORDER 18
#else
#define MAX_ORDER CONFIG_FORCE_MAX_ZONEORDER
#endif
diff --git a/include/linux/pgtable.h b/include/linux/pgtable.h
index e237004d4..6a3b7aefb 100644
--- a/include/linux/pgtable.h
+++ b/include/linux/pgtable.h
@@ -93,6 +93,14 @@ static inline pmd_t *pmd_offset(pud_t *pud, unsigned long address)
#define pmd_offset pmd_offset
#endif
+#ifndef pte_offset
+static inline pte_t *pte_offset(pmd_t *pmd, unsigned long address)
+{
+ return (pte_t *)pmd_page_vaddr(*pmd) + pte_index(address);
+}
+#define pte_offset pte_offset
+#endif
+
#ifndef pud_offset
static inline pud_t *pud_offset(p4d_t *p4d, unsigned long address)
{
@@ -240,6 +248,16 @@ static inline int pmdp_clear_flush_young(struct vm_area_struct *vma,
#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
#endif
+#ifdef CONFIG_PT_AREA_BATCH
+struct pt_area_batch_t {
+ unsigned long ptep_base;
+ union {
+ unsigned long ptep_size;
+ unsigned long ptep_entry;
+ } entity;
+};
+#endif
+
#ifndef __HAVE_ARCH_PTEP_GET_AND_CLEAR
static inline pte_t ptep_get_and_clear(struct mm_struct *mm,
unsigned long address,
@@ -249,6 +267,18 @@ static inline pte_t ptep_get_and_clear(struct mm_struct *mm,
pte_clear(mm, address, ptep);
return pte;
}
+
+#endif
+
+#ifdef CONFIG_PT_AREA_BATCH
+static inline pte_t delay2_ptep_get_and_clear(struct mm_struct *mm,
+ unsigned long address,
+ pte_t *ptep)
+{
+ pte_t pte = *ptep;
+ delay_pte_clear(mm, address, ptep);
+ return pte;
+}
#endif
#ifndef __HAVE_ARCH_PTEP_GET
@@ -311,6 +341,18 @@ static inline pte_t ptep_get_and_clear_full(struct mm_struct *mm,
pte = ptep_get_and_clear(mm, address, ptep);
return pte;
}
+
+#ifdef CONFIG_PT_AREA_BATCH
+static inline pte_t delay_ptep_get_and_clear_full(struct mm_struct *mm,
+ unsigned long address, pte_t *ptep,
+ int full)
+{
+ pte_t pte;
+ pte = delay2_ptep_get_and_clear(mm, address, ptep);
+ return pte;
+}
+#endif
+
#endif
diff --git a/include/linux/pt_area.h b/include/linux/pt_area.h
new file mode 100644
index 000000000..f54426211
--- /dev/null
+++ b/include/linux/pt_area.h
@@ -0,0 +1,25 @@
+#ifndef _LINUX_PT_AREA_H
+#define _LINUX_PT_AREA_H
+#define DEFAULT_PGD_PAGE_ORDER 5
+#define DEFAULT_PMD_PAGE_ORDER 7
+
+void init_pt_area(void);
+
+unsigned long pt_pages_num(void);
+
+unsigned long pt_free_pages_num(void);
+
+char* alloc_pt_pgd_page(void);
+
+char* alloc_pt_pmd_page(void);
+
+char* alloc_pt_pte_page(void);
+
+int free_pt_pgd_page(unsigned long page);
+
+int free_pt_pmd_page(unsigned long page);
+
+int free_pt_pte_page(unsigned long page);
+
+int check_pt_pte_page(unsigned long page);
+#endif /* _LINUX_PT_AREA_H */
diff --git a/init/main.c b/init/main.c
index 32b2a8aff..bb1282632 100644
--- a/init/main.c
+++ b/init/main.c
@@ -105,6 +105,13 @@
#include <asm/sections.h>
#include <asm/cacheflush.h>
+#ifdef CONFIG_PT_AREA
+#include <linux/pt_area.h>
+#include <asm/tlbflush.h>
+#include <asm/page.h>
+#endif
+
+
#define CREATE_TRACE_POINTS
#include <trace/events/initcall.h>
@@ -840,6 +847,81 @@ static void __init mm_init(void)
pti_init();
}
+#ifdef CONFIG_PT_AREA
+static void deep_copy_pt(pte_t* src_pt, pte_t* dest_pt, int level)
+{
+ unsigned long i=0;
+ int tmpLevel = level;
+ for(i=0;i<PTRS_PER_PGD;++i)
+ {
+ unsigned long pte=src_pt[i].pte;
+ if(pte & _PAGE_PRESENT)
+ {
+ if((pte & _PAGE_READ) || (pte & _PAGE_EXEC))
+ {
+ //Find a leaf PTE
+ dest_pt[i]=__pte(pte);
+ }
+ else
+ {
+ pte_t* new_dest_pt;
+ pte_t* new_src_pt;
+ if (tmpLevel == 0){
+ new_dest_pt=(pte_t*)alloc_pt_pmd_page();
+ }
+ else{
+ new_dest_pt=(pte_t*)alloc_pt_pte_page();
+ }
+ new_src_pt=(pte_t*)pfn_to_virt(pte>>_PAGE_PFN_SHIFT);
+ deep_copy_pt(new_src_pt, new_dest_pt,level+1);
+ src_pt[i]=pfn_pte(PFN_DOWN(__pa(new_dest_pt)), __pgprot(pte & 0x3FF));
+ dest_pt[i]=pfn_pte(PFN_DOWN(__pa(new_dest_pt)), __pgprot(pte & 0x3FF));
+ }
+ }
+ }
+}
+
+/*
+ * Transfer the swapper pg dir that kernel uses during boot to pt area
+ * This function can only be called after init_pt_area is called
+ */
+int enclave_module_installed = 0;
+EXPORT_SYMBOL(enclave_module_installed);
+extern unsigned long pt_area_vaddr;
+extern unsigned long pt_area_pages;
+static void transfer_init_pt(void)
+{
+ pte_t* new_swapper_pt;
+ unsigned long i=0;
+ pr_notice("Transfer init table\n");
+#ifndef __PAGETABLE_PMD_FOLDED
+ BUG_ON((PTRS_PER_PGD != PTRS_PER_PTE) || (PTRS_PER_PTE != PTRS_PER_PMD));
+#else
+ BUG_ON(PTRS_PER_PGD != PTRS_PER_PTE);
+#endif
+
+ //Actually here should be pgd_t or pmd_t or pte_t, just for simplicity
+ new_swapper_pt=(pte_t*)alloc_pt_pgd_page();
+ new_swapper_pg_dir=(void*)__pa(new_swapper_pt);
+ deep_copy_pt((pte_t*)swapper_pg_dir, new_swapper_pt,0);
+ mb();
+
+ pr_notice("Before transfer init table: sptbr is 0x%lx, init_mm.pgd is 0x%lx\n",csr_read(sptbr),(unsigned long)init_mm.pgd);
+ init_mm.pgd=(pgd_t*)new_swapper_pt;
+ csr_write(sptbr, virt_to_pfn(new_swapper_pt) | SATP_MODE);
+ local_flush_tlb_all();
+ pr_notice("After transfer init table: sptbr is 0x%lx, init_mm.pgd is 0x%lx\n",csr_read(sptbr),(unsigned long)init_mm.pgd);
+
+ //clear swapper_pg_dir
+ for(i=0;i<PTRS_PER_PGD;++i)
+ {
+ swapper_pg_dir[i].pgd=0;
+ }
+ enclave_module_installed = 0;
+}
+
+#endif /* CONFIG_PT_AREA */
+
void __init __weak arch_call_rest_init(void)
{
rest_init();
@@ -903,6 +985,10 @@ asmlinkage __visible void __init __no_sanitize_address start_kernel(void)
trap_init();
mm_init();
+ #ifdef CONFIG_PT_AREA
+ init_pt_area();
+ transfer_init_pt();
+ #endif
ftrace_init();
/* trace_printk can be enabled here */
diff --git a/mm/Makefile b/mm/Makefile
index d73aed0fc..6e3251c99 100644
--- a/mm/Makefile
+++ b/mm/Makefile
@@ -66,6 +66,10 @@ ifdef CONFIG_MMU
obj-$(CONFIG_ADVISE_SYSCALLS) += madvise.o
endif
+ifdef CONFIG_PT_AREA
+ obj-y += pt_area.o
+endif
+
obj-$(CONFIG_SWAP) += page_io.o swap_state.o swapfile.o swap_slots.o
obj-$(CONFIG_FRONTSWAP) += frontswap.o
obj-$(CONFIG_ZSWAP) += zswap.o
diff --git a/mm/filemap.c b/mm/filemap.c
index 0b2067b3c..f18df62fc 100644
--- a/mm/filemap.c
+++ b/mm/filemap.c
@@ -2884,8 +2884,21 @@ void filemap_map_pages(struct vm_fault *vmf,
if (vmf->pte)
vmf->pte += xas.xa_index - last_pgoff;
last_pgoff = xas.xa_index;
- if (alloc_set_pte(vmf, page))
- goto unlock;
+ #ifdef CONFIG_PT_AREA_BATCH
+ if(enclave_module_installed)
+ {
+ if (alloc_noset_pte(vmf, page))
+ goto unlock;
+ }
+ else
+ {
+ if (alloc_set_pte(vmf, page))
+ goto unlock;
+ }
+ #else
+ if (alloc_set_pte(vmf, page))
+ goto unlock;
+ #endif
unlock_page(head);
goto next;
unlock:
@@ -2897,6 +2910,10 @@ void filemap_map_pages(struct vm_fault *vmf,
if (pmd_trans_huge(*vmf->pmd))
break;
}
+ #ifdef CONFIG_PT_AREA_BATCH
+ if(enclave_module_installed)
+ flush_pt_area_set_buffer();
+ #endif
rcu_read_unlock();
WRITE_ONCE(file->f_ra.mmap_miss, mmap_miss);
}