arch/x86/coco/tdx/tdx.c

Source file repositories/reference/linux-study-clean/arch/x86/coco/tdx/tdx.c

File Facts

System
Linux kernel
Corpus path
arch/x86/coco/tdx/tdx.c
Extension
.c
Size
32295 bytes
Lines
1198
Domain
Architecture Layer
Bucket
arch/x86
Inferred role
Architecture Layer: exported/initcall integration point
Status
integration implementation candidate

Why This File Exists

CPU and platform-specific kernel glue: boot entry, traps, syscall entry, interrupts, page tables, context switch, and low-level barriers.

Dependency Surface

Detected Declarations

Annotated Snippet

if (map_fail_paddr == start) {
			retry_count++;
			continue;
		}

		start = map_fail_paddr;
		retry_count = 0;
	}

	return false;
}

/*
 * Inform the VMM of the guest's intent for this physical page: shared with
 * the VMM or private to the guest.  The VMM is expected to change its mapping
 * of the page in response.
 */
static bool tdx_enc_status_changed(unsigned long vaddr, int numpages, bool enc)
{
	phys_addr_t start = __pa(vaddr);
	phys_addr_t end   = __pa(vaddr + numpages * PAGE_SIZE);

	if (!tdx_map_gpa(start, end, enc))
		return false;

	/* shared->private conversion requires memory to be accepted before use */
	if (enc)
		return tdx_accept_memory(start, end);

	return true;
}

static int tdx_enc_status_change_prepare(unsigned long vaddr, int numpages,
					 bool enc)
{
	/*
	 * Only handle shared->private conversion here.
	 * See the comment in tdx_early_init().
	 */
	if (enc && !tdx_enc_status_changed(vaddr, numpages, enc))
		return -EIO;

	return 0;
}

static int tdx_enc_status_change_finish(unsigned long vaddr, int numpages,
					 bool enc)
{
	/*
	 * Only handle private->shared conversion here.
	 * See the comment in tdx_early_init().
	 */
	if (!enc && !tdx_enc_status_changed(vaddr, numpages, enc))
		return -EIO;

	if (enc)
		atomic_long_sub(numpages, &nr_shared);
	else
		atomic_long_add(numpages, &nr_shared);

	return 0;
}

/* Stop new private<->shared conversions */
static void tdx_kexec_begin(void)
{
	if (!IS_ENABLED(CONFIG_KEXEC_CORE))
		return;

	/*
	 * Crash kernel reaches here with interrupts disabled: can't wait for
	 * conversions to finish.
	 *
	 * If race happened, just report and proceed.
	 */
	if (!set_memory_enc_stop_conversion())
		pr_warn("Failed to stop shared<->private conversions\n");
}

/* Walk direct mapping and convert all shared memory back to private */
static void tdx_kexec_finish(void)
{
	unsigned long addr, end;
	long found = 0, shared;

	if (!IS_ENABLED(CONFIG_KEXEC_CORE))
		return;

	lockdep_assert_irqs_disabled();

Annotation

Implementation Notes