linux启动之进入C语言环境 arch/x86/kernel/head_64.S

kernel对Virtual Address Space的基本规划:

@see Documentation/x86/x86_64/mm.txt

ffff880000000000 - ffffc7ffffffffff (=64 TB) direct mapping of all phys. memory
init_level4_pgt(272)->level3_ident_pgt(0)->level2_ident_pgt(512*2M=1G)

ffffffff80000000 - ffffffffa0000000 (=512 MB) kernel text mapping, from phys 0
init_level4_pgt(511)->level3_kernel_pgt(510)->level2_kernel_pgt(256*2M=512M)

@see arch/x86/include/asm/page_64_types.h

#define __PAGE_OFFSET           _AC(0xffff880000000000, UL)
#define __PHYSICAL_START        CONFIG_PHYSICAL_START // 0x1000000 = 16M

#define __START_KERNEL_map  _AC(0xffffffff80000000, UL)
#define __START_KERNEL      (__START_KERNEL_map + __PHYSICAL_START) // 0xffffffff81000000

// Kernel image size is limited to 512 MB
#define KERNEL_IMAGE_SIZE   (512 * 1024 * 1024)
#define KERNEL_IMAGE_START  _AC(0xffffffff80000000, UL)
pagetable
init_level4_pgt (0x81a03000)
00x1a04063
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
2720x1a04063
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
5110x1a05067
level3_ident_pgt (0x81a04000)
00x1a08063
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
level3_kernel_pgt (0x81a05000)
0
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
5100x1a09063
5110x1a06067
level2_fixmap_pgt (0x81a06000)
0
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
5060x1a07067
507
508
509
510
511
level1_fixmap_pgt (0x81a07000)
0
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
level2_ident_pgt (0x81a08000)
00x1e3
10x2001e3
20x4001e3
30x6001e3
40x8001e3
50xa001e3
60xc001e3
70xe001e3
80x10001e3
90x12001e3
100x14001e3
110x16001e3
120x18001e3
130x1a001e3
140x1c001e3
150x1e001e3
160x20001e3
170x22001e3
180x24001e3
190x26001e3
200x28001e3
210x2a001e3
220x2c001e3
230x2e001e3
240x30001e3
250x32001e3
260x34001e3
270x36001e3
280x38001e3
290x3a001e3
300x3c001e3
310x3e001e3
320x40001e3
330x42001e3
340x44001e3
350x46001e3
360x48001e3
370x4a001e3
380x4c001e3
390x4e001e3
400x50001e3
410x52001e3
420x54001e3
430x56001e3
440x58001e3
450x5a001e3
460x5c001e3
470x5e001e3
480x60001e3
490x62001e3
500x64001e3
510x66001e3
520x68001e3
530x6a001e3
540x6c001e3
550x6e001e3
560x70001e3
570x72001e3
580x74001e3
590x76001e3
600x78001e3
610x7a001e3
620x7c001e3
630x7e001e3
640x80001e3
650x82001e3
660x84001e3
670x86001e3
680x88001e3
690x8a001e3
700x8c001e3
710x8e001e3
720x90001e3
730x92001e3
740x94001e3
750x96001e3
760x98001e3
770x9a001e3
780x9c001e3
790x9e001e3
800xa0001e3
810xa2001e3
820xa4001e3
830xa6001e3
840xa8001e3
850xaa001e3
860xac001e3
870xae001e3
880xb0001e3
890xb2001e3
900xb4001e3
910xb6001e3
920xb8001e3
930xba001e3
940xbc001e3
950xbe001e3
960xc0001e3
970xc2001e3
980xc4001e3
990xc6001e3
1000xc8001e3
1010xca001e3
1020xcc001e3
1030xce001e3
1040xd0001e3
1050xd2001e3
1060xd4001e3
1070xd6001e3
1080xd8001e3
1090xda001e3
1100xdc001e3
1110xde001e3
1120xe0001e3
1130xe2001e3
1140xe4001e3
1150xe6001e3
1160xe8001e3
1170xea001e3
1180xec001e3
1190xee001e3
1200xf0001e3
1210xf2001e3
1220xf4001e3
1230xf6001e3
1240xf8001e3
1250xfa001e3
1260xfc001e3
1270xfe001e3
1280x100001e3
1290x102001e3
1300x104001e3
1310x106001e3
1320x108001e3
1330x10a001e3
1340x10c001e3
1350x10e001e3
1360x110001e3
1370x112001e3
1380x114001e3
1390x116001e3
1400x118001e3
1410x11a001e3
1420x11c001e3
1430x11e001e3
1440x120001e3
1450x122001e3
1460x124001e3
1470x126001e3
1480x128001e3
1490x12a001e3
1500x12c001e3
1510x12e001e3
1520x130001e3
1530x132001e3
1540x134001e3
1550x136001e3
1560x138001e3
1570x13a001e3
1580x13c001e3
1590x13e001e3
1600x140001e3
1610x142001e3
1620x144001e3
1630x146001e3
1640x148001e3
1650x14a001e3
1660x14c001e3
1670x14e001e3
1680x150001e3
1690x152001e3
1700x154001e3
1710x156001e3
1720x158001e3
1730x15a001e3
1740x15c001e3
1750x15e001e3
1760x160001e3
1770x162001e3
1780x164001e3
1790x166001e3
1800x168001e3
1810x16a001e3
1820x16c001e3
1830x16e001e3
1840x170001e3
1850x172001e3
1860x174001e3
1870x176001e3
1880x178001e3
1890x17a001e3
1900x17c001e3
1910x17e001e3
1920x180001e3
1930x182001e3
1940x184001e3
1950x186001e3
1960x188001e3
1970x18a001e3
1980x18c001e3
1990x18e001e3
2000x190001e3
2010x192001e3
2020x194001e3
2030x196001e3
2040x198001e3
2050x19a001e3
2060x19c001e3
2070x19e001e3
2080x1a0001e3
2090x1a2001e3
2100x1a4001e3
2110x1a6001e3
2120x1a8001e3
2130x1aa001e3
2140x1ac001e3
2150x1ae001e3
2160x1b0001e3
2170x1b2001e3
2180x1b4001e3
2190x1b6001e3
2200x1b8001e3
2210x1ba001e3
2220x1bc001e3
2230x1be001e3
2240x1c0001e3
2250x1c2001e3
2260x1c4001e3
2270x1c6001e3
2280x1c8001e3
2290x1ca001e3
2300x1cc001e3
2310x1ce001e3
2320x1d0001e3
2330x1d2001e3
2340x1d4001e3
2350x1d6001e3
2360x1d8001e3
2370x1da001e3
2380x1dc001e3
2390x1de001e3
2400x1e0001e3
2410x1e2001e3
2420x1e4001e3
2430x1e6001e3
2440x1e8001e3
2450x1ea001e3
2460x1ec001e3
2470x1ee001e3
2480x1f0001e3
2490x1f2001e3
2500x1f4001e3
2510x1f6001e3
2520x1f8001e3
2530x1fa001e3
2540x1fc001e3
2550x1fe001e3
2560x200001e3
2570x202001e3
2580x204001e3
2590x206001e3
2600x208001e3
2610x20a001e3
2620x20c001e3
2630x20e001e3
2640x210001e3
2650x212001e3
2660x214001e3
2670x216001e3
2680x218001e3
2690x21a001e3
2700x21c001e3
2710x21e001e3
2720x220001e3
2730x222001e3
2740x224001e3
2750x226001e3
2760x228001e3
2770x22a001e3
2780x22c001e3
2790x22e001e3
2800x230001e3
2810x232001e3
2820x234001e3
2830x236001e3
2840x238001e3
2850x23a001e3
2860x23c001e3
2870x23e001e3
2880x240001e3
2890x242001e3
2900x244001e3
2910x246001e3
2920x248001e3
2930x24a001e3
2940x24c001e3
2950x24e001e3
2960x250001e3
2970x252001e3
2980x254001e3
2990x256001e3
3000x258001e3
3010x25a001e3
3020x25c001e3
3030x25e001e3
3040x260001e3
3050x262001e3
3060x264001e3
3070x266001e3
3080x268001e3
3090x26a001e3
3100x26c001e3
3110x26e001e3
3120x270001e3
3130x272001e3
3140x274001e3
3150x276001e3
3160x278001e3
3170x27a001e3
3180x27c001e3
3190x27e001e3
3200x280001e3
3210x282001e3
3220x284001e3
3230x286001e3
3240x288001e3
3250x28a001e3
3260x28c001e3
3270x28e001e3
3280x290001e3
3290x292001e3
3300x294001e3
3310x296001e3
3320x298001e3
3330x29a001e3
3340x29c001e3
3350x29e001e3
3360x2a0001e3
3370x2a2001e3
3380x2a4001e3
3390x2a6001e3
3400x2a8001e3
3410x2aa001e3
3420x2ac001e3
3430x2ae001e3
3440x2b0001e3
3450x2b2001e3
3460x2b4001e3
3470x2b6001e3
3480x2b8001e3
3490x2ba001e3
3500x2bc001e3
3510x2be001e3
3520x2c0001e3
3530x2c2001e3
3540x2c4001e3
3550x2c6001e3
3560x2c8001e3
3570x2ca001e3
3580x2cc001e3
3590x2ce001e3
3600x2d0001e3
3610x2d2001e3
3620x2d4001e3
3630x2d6001e3
3640x2d8001e3
3650x2da001e3
3660x2dc001e3
3670x2de001e3
3680x2e0001e3
3690x2e2001e3
3700x2e4001e3
3710x2e6001e3
3720x2e8001e3
3730x2ea001e3
3740x2ec001e3
3750x2ee001e3
3760x2f0001e3
3770x2f2001e3
3780x2f4001e3
3790x2f6001e3
3800x2f8001e3
3810x2fa001e3
3820x2fc001e3
3830x2fe001e3
3840x300001e3
3850x302001e3
3860x304001e3
3870x306001e3
3880x308001e3
3890x30a001e3
3900x30c001e3
3910x30e001e3
3920x310001e3
3930x312001e3
3940x314001e3
3950x316001e3
3960x318001e3
3970x31a001e3
3980x31c001e3
3990x31e001e3
4000x320001e3
4010x322001e3
4020x324001e3
4030x326001e3
4040x328001e3
4050x32a001e3
4060x32c001e3
4070x32e001e3
4080x330001e3
4090x332001e3
4100x334001e3
4110x336001e3
4120x338001e3
4130x33a001e3
4140x33c001e3
4150x33e001e3
4160x340001e3
4170x342001e3
4180x344001e3
4190x346001e3
4200x348001e3
4210x34a001e3
4220x34c001e3
4230x34e001e3
4240x350001e3
4250x352001e3
4260x354001e3
4270x356001e3
4280x358001e3
4290x35a001e3
4300x35c001e3
4310x35e001e3
4320x360001e3
4330x362001e3
4340x364001e3
4350x366001e3
4360x368001e3
4370x36a001e3
4380x36c001e3
4390x36e001e3
4400x370001e3
4410x372001e3
4420x374001e3
4430x376001e3
4440x378001e3
4450x37a001e3
4460x37c001e3
4470x37e001e3
4480x380001e3
4490x382001e3
4500x384001e3
4510x386001e3
4520x388001e3
4530x38a001e3
4540x38c001e3
4550x38e001e3
4560x390001e3
4570x392001e3
4580x394001e3
4590x396001e3
4600x398001e3
4610x39a001e3
4620x39c001e3
4630x39e001e3
4640x3a0001e3
4650x3a2001e3
4660x3a4001e3
4670x3a6001e3
4680x3a8001e3
4690x3aa001e3
4700x3ac001e3
4710x3ae001e3
4720x3b0001e3
4730x3b2001e3
4740x3b4001e3
4750x3b6001e3
4760x3b8001e3
4770x3ba001e3
4780x3bc001e3
4790x3be001e3
4800x3c0001e3
4810x3c2001e3
4820x3c4001e3
4830x3c6001e3
4840x3c8001e3
4850x3ca001e3
4860x3cc001e3
4870x3ce001e3
4880x3d0001e3
4890x3d2001e3
4900x3d4001e3
4910x3d6001e3
4920x3d8001e3
4930x3da001e3
4940x3dc001e3
4950x3de001e3
4960x3e0001e3
4970x3e2001e3
4980x3e4001e3
4990x3e6001e3
5000x3e8001e3
5010x3ea001e3
5020x3ec001e3
5030x3ee001e3
5040x3f0001e3
5050x3f2001e3
5060x3f4001e3
5070x3f6001e3
5080x3f8001e3
5090x3fa001e3
5100x3fc001e3
5110x3fe001e3
level2_kernel_pgt (0x81a09000)
00x1e3
10x2001e3
20x4001e3
30x6001e3
40x8001e3
50xa001e3
60xc001e3
70xe001e3
80x10001e3
90x12001e3
100x14001e3
110x16001e3
120x18001e3
130x1a001e3
140x1c001e3
150x1e001e3
160x20001e3
170x22001e3
180x24001e3
190x26001e3
200x28001e3
210x2a001e3
220x2c001e3
230x2e001e3
240x30001e3
250x32001e3
260x34001e3
270x36001e3
280x38001e3
290x3a001e3
300x3c001e3
310x3e001e3
320x40001e3
330x42001e3
340x44001e3
350x46001e3
360x48001e3
370x4a001e3
380x4c001e3
390x4e001e3
400x50001e3
410x52001e3
420x54001e3
430x56001e3
440x58001e3
450x5a001e3
460x5c001e3
470x5e001e3
480x60001e3
490x62001e3
500x64001e3
510x66001e3
520x68001e3
530x6a001e3
540x6c001e3
550x6e001e3
560x70001e3
570x72001e3
580x74001e3
590x76001e3
600x78001e3
610x7a001e3
620x7c001e3
630x7e001e3
640x80001e3
650x82001e3
660x84001e3
670x86001e3
680x88001e3
690x8a001e3
700x8c001e3
710x8e001e3
720x90001e3
730x92001e3
740x94001e3
750x96001e3
760x98001e3
770x9a001e3
780x9c001e3
790x9e001e3
800xa0001e3
810xa2001e3
820xa4001e3
830xa6001e3
840xa8001e3
850xaa001e3
860xac001e3
870xae001e3
880xb0001e3
890xb2001e3
900xb4001e3
910xb6001e3
920xb8001e3
930xba001e3
940xbc001e3
950xbe001e3
960xc0001e3
970xc2001e3
980xc4001e3
990xc6001e3
1000xc8001e3
1010xca001e3
1020xcc001e3
1030xce001e3
1040xd0001e3
1050xd2001e3
1060xd4001e3
1070xd6001e3
1080xd8001e3
1090xda001e3
1100xdc001e3
1110xde001e3
1120xe0001e3
1130xe2001e3
1140xe4001e3
1150xe6001e3
1160xe8001e3
1170xea001e3
1180xec001e3
1190xee001e3
1200xf0001e3
1210xf2001e3
1220xf4001e3
1230xf6001e3
1240xf8001e3
1250xfa001e3
1260xfc001e3
1270xfe001e3
1280x100001e3
1290x102001e3
1300x104001e3
1310x106001e3
1320x108001e3
1330x10a001e3
1340x10c001e3
1350x10e001e3
1360x110001e3
1370x112001e3
1380x114001e3
1390x116001e3
1400x118001e3
1410x11a001e3
1420x11c001e3
1430x11e001e3
1440x120001e3
1450x122001e3
1460x124001e3
1470x126001e3
1480x128001e3
1490x12a001e3
1500x12c001e3
1510x12e001e3
1520x130001e3
1530x132001e3
1540x134001e3
1550x136001e3
1560x138001e3
1570x13a001e3
1580x13c001e3
1590x13e001e3
1600x140001e3
1610x142001e3
1620x144001e3
1630x146001e3
1640x148001e3
1650x14a001e3
1660x14c001e3
1670x14e001e3
1680x150001e3
1690x152001e3
1700x154001e3
1710x156001e3
1720x158001e3
1730x15a001e3
1740x15c001e3
1750x15e001e3
1760x160001e3
1770x162001e3
1780x164001e3
1790x166001e3
1800x168001e3
1810x16a001e3
1820x16c001e3
1830x16e001e3
1840x170001e3
1850x172001e3
1860x174001e3
1870x176001e3
1880x178001e3
1890x17a001e3
1900x17c001e3
1910x17e001e3
1920x180001e3
1930x182001e3
1940x184001e3
1950x186001e3
1960x188001e3
1970x18a001e3
1980x18c001e3
1990x18e001e3
2000x190001e3
2010x192001e3
2020x194001e3
2030x196001e3
2040x198001e3
2050x19a001e3
2060x19c001e3
2070x19e001e3
2080x1a0001e3
2090x1a2001e3
2100x1a4001e3
2110x1a6001e3
2120x1a8001e3
2130x1aa001e3
2140x1ac001e3
2150x1ae001e3
2160x1b0001e3
2170x1b2001e3
2180x1b4001e3
2190x1b6001e3
2200x1b8001e3
2210x1ba001e3
2220x1bc001e3
2230x1be001e3
2240x1c0001e3
2250x1c2001e3
2260x1c4001e3
2270x1c6001e3
2280x1c8001e3
2290x1ca001e3
2300x1cc001e3
2310x1ce001e3
2320x1d0001e3
2330x1d2001e3
2340x1d4001e3
2350x1d6001e3
2360x1d8001e3
2370x1da001e3
2380x1dc001e3
2390x1de001e3
2400x1e0001e3
2410x1e2001e3
2420x1e4001e3
2430x1e6001e3
2440x1e8001e3
2450x1ea001e3
2460x1ec001e3
2470x1ee001e3
2480x1f0001e3
2490x1f2001e3
2500x1f4001e3
2510x1f6001e3
2520x1f8001e3
2530x1fa001e3
2540x1fc001e3
2550x1fe001e3
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
level2_spare_pgt (0x81a0a000)
0
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

startup_64

在head_64.S里写死的pagetable map了0xffff880000000000开头的1GB内存(level2_ident_pgt) 和 从0xffffffff80000000到0xffffffffa0000000的512M内存(level2_kernel_pgt).
init_level4_pgt -> level3_ident_pgt -> level2_ident_pgt
init_level4_pgt -> level3_kernel_pgt -> level2_kernel_pgt
level2_fixmap_pgt, level1_fixmap_pgt, level2_spare_pgt
编译时假设了vmlinux会被加载到内存16M处,如果不是,就需要修正pagetable.
由于写死的pagetable只map了开头的1GB内存,如果vmlinux被加载到1GB以上的位置,那就需要再多map出一些内存,当然首先得知道要map哪段内存,计算出pud_index即可.然后将 level2_spare_pgt 加到 level3_ident_pgt 里, 将加载位置开头的2M内存map(pmd_index)设到 level2_spare_pgt 里. (level2_spare_pgt的spare原来是这个意思)
接着在ident_complete里继续修正level2_kernel_pgt,我们知道level2_kernel_pgt只用了前256项,后256项是空着的,修正时先测试每一项(8字节)的最后一个bit是否为1,如果是,则修正,否则,跳过去.
修正完level2_kernel_pgt,接着修正 phys_base 和 trampoline_level4_pgt, trampoline不清楚是做什么用的.

secondary_startup_64

我们在startup_32里已经enable了PAE,这里把PGE也Enable了,然后把init_level4_pgt load进CR3,这样我们就用上了新的Pagetable,并且 level2_kernel_pgt 和 level2_ident_pgt 的 G bit 也起上了作用.
0x1e3 = 0001 1110 0011b (G bit = 1)

关于PGE的一些说明

The processor invalidates the TLB whenever CR3 is loaded either explicitly or implicitly. After the TLB is invalidated, subsequent address references can consume many clock cycles until their translations are cached as new entries in the TLB. Invalidation of TLB entries for frequently-used or critical pages can be avoided by specifying the translations for those pages as global. TLB entries for global pages are not invalidated as a result of a CR3 load. Global pages are invalidated using the INVLPG instruction.
Global-page extensions are controlled by setting and clearing the PGE bit in CR4 (bit 7). When CR4.PGE is set to 1, global-page extensions are enabled. When CR4.PGE is cleared to 0, global-page extensions are disabled. When CR4.PGE=1, setting the global (G) bit in the translation-table entry marks the page as global.
The INVLPG instruction ignores the G bit and can be used to invalidate individual global-page entries in the TLB. To invalidate all entries, including global-page entries, disable global-page extensions (CR4.PGE=0).
接下来Enable了System Call和No Execute(如果支持的话).

关于SCE

System-Call Extension (SCE) Bit. Setting this bit to 1 enables the SYSCALL and SYSRET instructions. Application software can use these instructions for low-latency system calls and returns in a non-segmented (flat) address space.

关于NXE,前边的pagetable的63位都是0,也就是说,map的内存里的代码是可执行的

No Execute (NX) Bit. Bit 63. This bit is present in the translation-table entries defined for PAE paging.
This bit controls the ability to execute code from all physical pages mapped by the table entry. For example, a page-map level-4 NX bit controls the ability to execute code from all 128M (512 × 512 × 512) physical pages it maps through the lower-level translation tables. When the NX bit is cleared to 0, code can be executed from the mapped physical pages. When the NX bit is set to 1, code cannot be executed from the mapped physical pages.
到目前为止,我们的所有操作都没有用到栈,但栈一定要设的:
movq stack_start(%rip), %rsp

ENTRY(stack_start)
    .quad  init_thread_union+THREAD_SIZE-8
    .word  0
我们看到,%rsp设到了init_thread_union的stack上. 这个后边再细说.

接着,load了新的gdt. 现在的gdt是在startup_32时load的,注释里说的不太明白怎么回事.
新的gdt定义在arch/x86/kernel/cpu/common.c#0138,显然是一个cpu有一个.

gdt ready之后,重置了ds,ss,es,我们知道64bit-mode下这三个段寄存器没用处,fs和gs有用,但目前不太了解,略过.

最后,再次使用lret跳转到C语言入口函数 x86_64_start_kernel(char *real_mode_data),并设置好了正确的CS(我们load了新的gdt).

OK,到此,我们算是进入了C语言环境,接下来的kernel代码就是C语言的了,易读多了.