@@ -311,59 +311,82 @@ static int amd_enc_status_change_finish(unsigned long vaddr, int npages, bool en
311
311
return 0 ;
312
312
}
313
313
314
- static void __init __set_clr_pte_enc ( pte_t * kpte , int level , bool enc )
314
+ int prepare_pte_enc ( struct pte_enc_desc * d )
315
315
{
316
- pgprot_t old_prot , new_prot ;
317
- unsigned long pfn , pa , size ;
318
- pte_t new_pte ;
316
+ pgprot_t old_prot ;
319
317
320
- pfn = pg_level_to_pfn (level , kpte , & old_prot );
321
- if (!pfn )
322
- return ;
318
+ d -> pfn = pg_level_to_pfn (d -> pte_level , d -> kpte , & old_prot );
319
+ if (!d -> pfn )
320
+ return 1 ;
323
321
324
- new_prot = old_prot ;
325
- if (enc )
326
- pgprot_val (new_prot ) |= _PAGE_ENC ;
322
+ d -> new_pgprot = old_prot ;
323
+ if (d -> encrypt )
324
+ pgprot_val (d -> new_pgprot ) |= _PAGE_ENC ;
327
325
else
328
- pgprot_val (new_prot ) &= ~_PAGE_ENC ;
326
+ pgprot_val (d -> new_pgprot ) &= ~_PAGE_ENC ;
329
327
330
328
/* If prot is same then do nothing. */
331
- if (pgprot_val (old_prot ) == pgprot_val (new_prot ))
332
- return ;
329
+ if (pgprot_val (old_prot ) == pgprot_val (d -> new_pgprot ))
330
+ return 1 ;
333
331
334
- pa = pfn << PAGE_SHIFT ;
335
- size = page_level_size (level );
332
+ d -> pa = d -> pfn << PAGE_SHIFT ;
333
+ d -> size = page_level_size (d -> pte_level );
336
334
337
335
/*
338
- * We are going to perform in-place en-/decryption and change the
339
- * physical page attribute from C=1 to C=0 or vice versa. Flush the
340
- * caches to ensure that data gets accessed with the correct C-bit.
336
+ * In-place en-/decryption and physical page attribute change
337
+ * from C=1 to C=0 or vice versa will be performed. Flush the
338
+ * caches to ensure that data gets accessed with the correct
339
+ * C-bit.
341
340
*/
342
- clflush_cache_range (__va (pa ), size );
341
+ if (d -> va )
342
+ clflush_cache_range (d -> va , d -> size );
343
+ else
344
+ clflush_cache_range (__va (d -> pa ), d -> size );
345
+
346
+ return 0 ;
347
+ }
348
+
349
+ void set_pte_enc_mask (pte_t * kpte , unsigned long pfn , pgprot_t new_prot )
350
+ {
351
+ pte_t new_pte ;
352
+
353
+ /* Change the page encryption mask. */
354
+ new_pte = pfn_pte (pfn , new_prot );
355
+ set_pte_atomic (kpte , new_pte );
356
+ }
357
+
358
+ static void __init __set_clr_pte_enc (pte_t * kpte , int level , bool enc )
359
+ {
360
+ struct pte_enc_desc d = {
361
+ .kpte = kpte ,
362
+ .pte_level = level ,
363
+ .encrypt = enc
364
+ };
365
+
366
+ if (prepare_pte_enc (& d ))
367
+ return ;
343
368
344
369
/* Encrypt/decrypt the contents in-place */
345
370
if (enc ) {
346
- sme_early_encrypt (pa , size );
371
+ sme_early_encrypt (d . pa , d . size );
347
372
} else {
348
- sme_early_decrypt (pa , size );
373
+ sme_early_decrypt (d . pa , d . size );
349
374
350
375
/*
351
376
* ON SNP, the page state in the RMP table must happen
352
377
* before the page table updates.
353
378
*/
354
- early_snp_set_memory_shared ((unsigned long )__va (pa ), pa , 1 );
379
+ early_snp_set_memory_shared ((unsigned long )__va (d . pa ), d . pa , 1 );
355
380
}
356
381
357
- /* Change the page encryption mask. */
358
- new_pte = pfn_pte (pfn , new_prot );
359
- set_pte_atomic (kpte , new_pte );
382
+ set_pte_enc_mask (kpte , d .pfn , d .new_pgprot );
360
383
361
384
/*
362
385
* If page is set encrypted in the page table, then update the RMP table to
363
386
* add this page as private.
364
387
*/
365
388
if (enc )
366
- early_snp_set_memory_private ((unsigned long )__va (pa ), pa , 1 );
389
+ early_snp_set_memory_private ((unsigned long )__va (d . pa ), d . pa , 1 );
367
390
}
368
391
369
392
static int __init early_set_memory_enc_dec (unsigned long vaddr ,
0 commit comments