Skip to content

Conversation

@Amannix
Copy link
Contributor

@Amannix Amannix commented Aug 26, 2024

Description:

When qemu is started, it can access different key spaces by specifying vid (virtual ID).
When the guest accesses the TKM, it will notify KVM through hypercall call, and KVM will send the guest's command data to the PSP for processing.

Related qemu PR: 支持海光可信密钥管理(TKM)的密钥隔离功能

xiongmengbiao and others added 8 commits August 21, 2024 16:55
…rnel space

hygon inclusion
category: feature
---------------------------

Add a self-defined mutex to support concurrent psp access between kernel
space and user space.

Signed-off-by: xiongmengbiao <xiongmengbiao@hygon.cn>
hygon inclusion
category: feature
---------------------------

Add ioctl interface to control the state of self-defined
mutex in user and kernel space.

By default, when psp user-mode driver is not used,
the self-defined mutex is disabled, and the kernel's native
private lock is utilized instead.

Signed-off-by: xiongmengbiao <xiongmengbiao@hygon.cn>
hygon inclusion
category: feature
---------------------------

Support the PSP virtualization basic framework.

The guest uses the vmmcall instruction to
interact with KVM, which then forwards
the data to the PSP device driver and sends
it to the PSP hardware.

Signed-off-by: niuyongwen <niuyongwen@hygon.cn>
hygon inclusion
category: feature
---------------------------

Allow the guest to execute Trusted Key Management (TKM)
commands in user mode.

Each TKM command data block is transferred to KVM using
the vmmcall instruction and processed through the following
three steps:

1. Obtain the VM command and preprocess the pointer
   mapping table information in the command buffer
2. The command that has been converted will interact
   with the channel of the psp through the driver and
   try to obtain the execution result
3. The executed command data is recovered according to
   the multilevel pointer of the mapping table, and then returned to the VM

Signed-off-by: niuyongwen <niuyongwen@hygon.cn>
hygon inclusion
category: feature
---------------------------

Add `vpsp_add_vid` and `vpsp_del_vid` to receive
VID information in host user mode.
Generally, these ioctl calls should be initiated from the QEMU process.

When sending data to the PSP hardware,
place the VID in the bit 56 to bit 63 range of the physical address.
The PSP hardware will then access different key spaces based on the VID.

Signed-off-by: xiongmengbiao <xiongmengbiao@hygon.cn>
hygon inclusion
category: feature
---------------------------

If the guest does not explicitly specify a VID
via `vpsp_add_vid`, VID 0 will be used by default,
sharing the key space with the host.

A `vpsp_set_default_vid_permission` ioctl call
has been added to control the behavior of the default VID.
If the default VID permission is set to "not allowed,"
any guest that does not explicitly specify a VID
will be denied access to the TKM function.

Signed-off-by: xiongmengbiao <xiongmengbiao@hygon.cn>
… module

hygon inclusion
category: feature
---------------------------

Because the KVM module calls certain interfaces from the ccp module,
such as vpsp_try_do_cmd, it is necessary to load the ccp module
before loading kvm.

However, on CPUs other than Hygon, the ccp module might not be loaded,
which would prevent the kvm module from loading.

Therefore, we use function hooks to call functions from the ccp module.
Now the module dependencies are as follows:

[root@centos-7-8 ~]# lsmod | grep kvm
kvm_amd               200704  0
kvm                  1339392  1 kvm_amd
ccp                   352256  1 kvm_amd
irqbypass              12288  2 vfio_pci_core,kvm

Signed-off-by: xiongmengbiao <xiongmengbiao@hygon.cn>
…emmory for tkm

hygon inclusion
category: bugfix
---------------------------

When deleting a VID, `memcpy` is used to move
data within the array and overwrite the deleted VID entry.

However, `memcpy` does not handle overlapping
memory regions correctly within the array.

Therefore, `memmove` should be used instead.

Signed-off-by: niuyongwen <niuyongwen@hygon.cn>
@deepin-ci-robot
Copy link

Hi @Amannix. Thanks for your PR. 😃

@deepin-ci-robot
Copy link

Hi @Amannix. Thanks for your PR.

I'm waiting for a deepin-community member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Details

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@deepin-ci-robot
Copy link

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:
Once this PR has been reviewed and has the lgtm label, please assign opsiff for approval. For more information see the Code Review Process.

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@Avenger-285714 Avenger-285714 merged commit e9307f8 into deepin-community:linux-6.6.y Aug 27, 2024
opsiff pushed a commit to opsiff/UOS-kernel that referenced this pull request Jan 5, 2026
…rnel space

hygon inclusion
category: feature
---------------------------

Add a self-defined mutex to support concurrent psp access between kernel
space and user space.

Signed-off-by: xiongmengbiao <xiongmengbiao@hygon.cn>
[move mutex patch sev_platform_shutdown to sev_firmware_shutdown
@@ -2838,9 +2866,18 @@ static void __sev_firmware_shutdown(struct sev_device *sev, bool panic)
 static void sev_firmware_shutdown(struct sev_device *sev)
 {
-       mutex_lock(&sev_cmd_mutex);
+       if (is_vendor_hygon()) {
+                       if (psp_mutex_lock_timeout(&hygon_psp_hooks.psp_misc->data_pg_aligned->mb_mutex,
+                                       PSP_MUTEX_TIMEOUT) != 1)
+                       return -EBUSY;
+       } else {
:
]
Link: deepin-community#386
(cherry picked from commit 75f7390)

Signed-off-by: Wentao Guan <guanwentao@uniontech.com>

Conflicts:
	drivers/crypto/ccp/sev-dev.c
opsiff pushed a commit to opsiff/UOS-kernel that referenced this pull request Jan 5, 2026
hygon inclusion
category: feature
---------------------------

Add ioctl interface to control the state of self-defined
mutex in user and kernel space.

By default, when psp user-mode driver is not used,
the self-defined mutex is disabled, and the kernel's native
private lock is utilized instead.

Signed-off-by: xiongmengbiao <xiongmengbiao@hygon.cn>
Link: deepin-community#386
(cherry picked from commit 87c2f52)
Signed-off-by: Wentao Guan <guanwentao@uniontech.com>

Conflicts:
	drivers/crypto/ccp/psp-dev.c
	drivers/crypto/ccp/sev-dev.c
opsiff pushed a commit to opsiff/UOS-kernel that referenced this pull request Jan 5, 2026
hygon inclusion
category: feature
---------------------------

Support the PSP virtualization basic framework.

The guest uses the vmmcall instruction to
interact with KVM, which then forwards
the data to the PSP device driver and sends
it to the PSP hardware.

Signed-off-by: niuyongwen <niuyongwen@hygon.cn>
Link: deepin-community#386
(cherry picked from commit abe09b8)
Signed-off-by: Wentao Guan <guanwentao@uniontech.com>

Conflicts:
	arch/x86/kvm/Makefile
	arch/x86/kvm/x86.c
opsiff pushed a commit to opsiff/UOS-kernel that referenced this pull request Jan 5, 2026
hygon inclusion
category: feature
---------------------------

Allow the guest to execute Trusted Key Management (TKM)
commands in user mode.

Each TKM command data block is transferred to KVM using
the vmmcall instruction and processed through the following
three steps:

1. Obtain the VM command and preprocess the pointer
   mapping table information in the command buffer
2. The command that has been converted will interact
   with the channel of the psp through the driver and
   try to obtain the execution result
3. The executed command data is recovered according to
   the multilevel pointer of the mapping table, and then returned to the VM

Signed-off-by: niuyongwen <niuyongwen@hygon.cn>
Link: deepin-community#386
(cherry picked from commit 907e2fa)
Signed-off-by: Wentao Guan <guanwentao@uniontech.com>
opsiff pushed a commit to opsiff/UOS-kernel that referenced this pull request Jan 5, 2026
hygon inclusion
category: feature
---------------------------

Add `vpsp_add_vid` and `vpsp_del_vid` to receive
VID information in host user mode.
Generally, these ioctl calls should be initiated from the QEMU process.

When sending data to the PSP hardware,
place the VID in the bit 56 to bit 63 range of the physical address.
The PSP hardware will then access different key spaces based on the VID.

Signed-off-by: xiongmengbiao <xiongmengbiao@hygon.cn>
Link: deepin-community#386
(cherry picked from commit f5402ac)
Signed-off-by: Wentao Guan <guanwentao@uniontech.com>

Conflicts:
	drivers/crypto/ccp/hygon/psp-dev.c
opsiff pushed a commit to opsiff/UOS-kernel that referenced this pull request Jan 5, 2026
hygon inclusion
category: feature
---------------------------

If the guest does not explicitly specify a VID
via `vpsp_add_vid`, VID 0 will be used by default,
sharing the key space with the host.

A `vpsp_set_default_vid_permission` ioctl call
has been added to control the behavior of the default VID.
If the default VID permission is set to "not allowed,"
any guest that does not explicitly specify a VID
will be denied access to the TKM function.

Signed-off-by: xiongmengbiao <xiongmengbiao@hygon.cn>
Link: deepin-community#386
(cherry picked from commit 726db88)
Signed-off-by: Wentao Guan <guanwentao@uniontech.com>
opsiff pushed a commit to opsiff/UOS-kernel that referenced this pull request Jan 5, 2026
… module

hygon inclusion
category: feature
---------------------------

Because the KVM module calls certain interfaces from the ccp module,
such as vpsp_try_do_cmd, it is necessary to load the ccp module
before loading kvm.

However, on CPUs other than Hygon, the ccp module might not be loaded,
which would prevent the kvm module from loading.

Therefore, we use function hooks to call functions from the ccp module.
Now the module dependencies are as follows:

[root@centos-7-8 ~]# lsmod | grep kvm
kvm_amd               200704  0
kvm                  1339392  1 kvm_amd
ccp                   352256  1 kvm_amd
irqbypass              12288  2 vfio_pci_core,kvm

Signed-off-by: xiongmengbiao <xiongmengbiao@hygon.cn>
Link: deepin-community#386
(cherry picked from commit d97fda9)
Signed-off-by: Wentao Guan <guanwentao@uniontech.com>

Conflicts:
	arch/x86/kvm/Makefile
opsiff pushed a commit to opsiff/UOS-kernel that referenced this pull request Jan 5, 2026
…emmory for tkm

hygon inclusion
category: bugfix
---------------------------

When deleting a VID, `memcpy` is used to move
data within the array and overwrite the deleted VID entry.

However, `memcpy` does not handle overlapping
memory regions correctly within the array.

Therefore, `memmove` should be used instead.

Signed-off-by: niuyongwen <niuyongwen@hygon.cn>
Link: deepin-community#386
(cherry picked from commit a67f9aa)
Signed-off-by: Wentao Guan <guanwentao@uniontech.com>
opsiff added a commit to opsiff/UOS-kernel that referenced this pull request Jan 5, 2026
Log:
[fix psp-dev.c
    drivers/crypto/ccp/hygon/psp-dev.c:25:10: error: no previous prototype for ‘atomic64_exchange’ [-Werror=missing-prototypes]
       25 | uint64_t atomic64_exchange(uint64_t *dst, uint64_t val)
          |          ^~~~~~~~~~~~~~~~~
    drivers/crypto/ccp/hygon/psp-dev.c:30:5: error: no previous prototype for ‘psp_mutex_init’ [-Werror=missing-prototypes]
       30 | int psp_mutex_init(struct psp_mutex *mutex)
          |     ^~~~~~~~~~~~~~
drivers/crypto/ccp/hygon/vpsp.c:108:21: error: no previous prototype for ‘map_tbl_dump’ [-Werror=missing-prototypes]
  108 | void __maybe_unused map_tbl_dump(const char *title, struct addr_map_tbls *tbls)
      |                     ^~~~~~~~~~~~
drivers/crypto/ccp/hygon/vpsp.c:121:21: error: no previous prototype for ‘g2h_tbl_dump’ [-Werror=missing-prototypes]
  121 | void __maybe_unused g2h_tbl_dump(const char *title, struct gpa2hva_tbls *tbls)
      |                     ^~~~~~~~~~~~
cc1: all warnings being treated as errors
make[5]: *** [scripts/Makefile.build:287:drivers/crypto/ccp/hygon/vpsp.o] 错误 1
make[5]: *** 正在等待未完成的任务....
drivers/crypto/ccp/hygon/psp-dev.c:188:6: error: no previous prototype for ‘vpsp_set_default_vid_permission’ [-Werror=missing-prototypes]
  188 | void vpsp_set_default_vid_permission(uint32_t is_allow)
      |      ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
drivers/crypto/ccp/hygon/psp-dev.c:495:5: error: no previous prototype for ‘__vpsp_do_cmd_locked’ [-Werror=missing-prototypes]
  495 | int __vpsp_do_cmd_locked(uint32_t vid, int cmd, void *data, int *psp_ret)
      |     ^~~~~~~~~~~~~~~~~~~~

cc1: all warnings being treated as errors ]

Link: deepin-community#386
Signed-off-by: Wentao Guan <guanwentao@uniontech.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants