Skip to content

Commit

Permalink
optee: close opened sessions
Browse files Browse the repository at this point in the history
Provide a way to close already opened sessions in last time.

Tracked-On: OAM-121160
Signed-off-by: Jingdong Lu <jingdong.lu@intel.com>
  • Loading branch information
jingdlu committed Jul 22, 2024
1 parent b37aed8 commit 009fcbe
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 0 deletions.
18 changes: 18 additions & 0 deletions drivers/tee/optee/call.c
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,24 @@ int optee_close_session(struct tee_context *ctx, u32 session)
return 0;
}

int optee_close_opened_session(struct tee_context *ctx, u32 session)
{
struct tee_shm *shm;
struct optee_msg_arg *msg_arg;
phys_addr_t msg_parg;

shm = get_msg_arg(ctx, 0, &msg_arg, &msg_parg);
if (IS_ERR(shm))
return PTR_ERR(shm);

msg_arg->cmd = OPTEE_MSG_CMD_CLOSE_SESSION;
msg_arg->session = session;
optee_do_call_with_arg(ctx, msg_parg);

tee_shm_free(shm);
return 0;
}

int optee_invoke_func(struct tee_context *ctx, struct tee_ioctl_invoke_arg *arg,
struct tee_param *param)
{
Expand Down
48 changes: 48 additions & 0 deletions drivers/tee/optee/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,28 @@ static struct tee_shm_pool *optee_config_dyn_shm(void)
return rc;
}

static unsigned long optee_msg_get_opened_session(optee_invoke_fn *invoke_fn)
{
union {
struct arm_smccc_res smccc;
struct optee_smc_call_get_opened_session_result result;
} res = {
.result = {
.session_id = 0
}
};

invoke_fn(OPTEE_SMC_GET_OPENED_SESSION, 0, 0, 0, 0, 0, 0, 0,
&res.smccc);

if (res.result.status != OPTEE_SMC_RETURN_OK)
return 0;

pr_info("get opened session %lu", res.result.session_id);

return res.result.session_id;
}

static struct tee_shm_pool *
optee_config_shm_memremap(optee_invoke_fn *invoke_fn, void **memremaped_shm)
{
Expand Down Expand Up @@ -952,6 +974,14 @@ static int optee_remove(struct platform_device *pdev)
return 0;
}

static int optee_ctx_match(struct tee_ioctl_version_data *ver, const void *data)
{
if (ver->impl_id == TEE_IMPL_ID_OPTEE)
return 1;
else
return 0;
}

static int optee_probe(struct platform_device *pdev)
{
optee_invoke_fn *invoke_fn;
Expand All @@ -963,6 +993,8 @@ static int optee_probe(struct platform_device *pdev)
int rc;

#if defined(CONFIG_X86_64)
unsigned long session_id = 0;
struct tee_context *ctx = NULL;
#if defined(CONFIG_OPTEE_VSOCK)
union {
struct sockaddr sa;
Expand Down Expand Up @@ -1089,6 +1121,22 @@ static int optee_probe(struct platform_device *pdev)

platform_set_drvdata(pdev, optee);

#if defined(CONFIG_X86_64)
ctx = tee_client_open_context(NULL, optee_ctx_match, NULL, NULL);
if (IS_ERR(ctx)) {
pr_err("Failed to open context");
optee_remove(pdev);
return -ENODEV;
}

while ((session_id = optee_msg_get_opened_session(invoke_fn))) {
pr_info("need to close session %lu", session_id);
optee_close_opened_session(ctx, session_id);
}

tee_client_close_context(ctx);
#endif

rc = optee_enumerate_devices(PTA_CMD_GET_DEVICES);
if (rc) {
optee_remove(pdev);
Expand Down
1 change: 1 addition & 0 deletions drivers/tee/optee/optee_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ int optee_open_session(struct tee_context *ctx,
struct tee_ioctl_open_session_arg *arg,
struct tee_param *param);
int optee_close_session(struct tee_context *ctx, u32 session);
int optee_close_opened_session(struct tee_context *ctx, u32 session);
int optee_invoke_func(struct tee_context *ctx, struct tee_ioctl_invoke_arg *arg,
struct tee_param *param);
int optee_cancel_req(struct tee_context *ctx, u32 cancel_id, u32 session);
Expand Down
26 changes: 26 additions & 0 deletions drivers/tee/optee/optee_smc.h
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,32 @@ struct optee_smc_disable_shm_cache_result {
#define OPTEE_SMC_GET_THREAD_COUNT \
OPTEE_SMC_FAST_CALL_VAL(OPTEE_SMC_FUNCID_GET_THREAD_COUNT)

/*
* Query OP-TEE about openend sessions info
*
* Call requests usage:
* a0 SMC Function ID, OPTEE_SMC_GET_OPENED_SESSION
* a1-6 Not used
* a7 Hypervisor Client ID register
*
* Normal return register usage:
* a0 OPTEE_SMC_RETURN_OK
* a1 Opened session id
* a2-7 Preserved
*
* Error return:
* a0 OPTEE_SMC_RETURN_UNKNOWN_FUNCTION Requested call is not implemented
* a1-7 Preserved
*/
#define OPTEE_SMC_FUNCID_GET_OPENED_SESSION 16
#define OPTEE_SMC_GET_OPENED_SESSION \
OPTEE_SMC_FAST_CALL_VAL(OPTEE_SMC_FUNCID_GET_OPENED_SESSION)

struct optee_smc_call_get_opened_session_result {
unsigned long status;
unsigned long session_id;
};

/*
* Resume from RPC (for example after processing a foreign interrupt)
*
Expand Down

0 comments on commit 009fcbe

Please sign in to comment.