Skip to content

Commit 8123073

Browse files
jarkkojshansendc
authored andcommitted
x86/sgx: Export sgx_encl_page_alloc()
Move sgx_encl_page_alloc() to encl.c and export it so that it can be used in the implementation for support of adding pages to initialized enclaves, which requires to allocate new enclave pages. Signed-off-by: Jarkko Sakkinen <jarkko@kernel.org> Signed-off-by: Reinette Chatre <reinette.chatre@intel.com> Signed-off-by: Dave Hansen <dave.hansen@linux.intel.com> Link: https://lkml.kernel.org/r/57ae71b4ea17998467670232e12d6617b95c6811.1652137848.git.reinette.chatre@intel.com
1 parent 3a53514 commit 8123073

3 files changed

Lines changed: 35 additions & 32 deletions

File tree

arch/x86/kernel/cpu/sgx/encl.c

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -887,6 +887,38 @@ int sgx_encl_test_and_clear_young(struct mm_struct *mm,
887887
return ret;
888888
}
889889

890+
struct sgx_encl_page *sgx_encl_page_alloc(struct sgx_encl *encl,
891+
unsigned long offset,
892+
u64 secinfo_flags)
893+
{
894+
struct sgx_encl_page *encl_page;
895+
unsigned long prot;
896+
897+
encl_page = kzalloc(sizeof(*encl_page), GFP_KERNEL);
898+
if (!encl_page)
899+
return ERR_PTR(-ENOMEM);
900+
901+
encl_page->desc = encl->base + offset;
902+
encl_page->encl = encl;
903+
904+
prot = _calc_vm_trans(secinfo_flags, SGX_SECINFO_R, PROT_READ) |
905+
_calc_vm_trans(secinfo_flags, SGX_SECINFO_W, PROT_WRITE) |
906+
_calc_vm_trans(secinfo_flags, SGX_SECINFO_X, PROT_EXEC);
907+
908+
/*
909+
* TCS pages must always RW set for CPU access while the SECINFO
910+
* permissions are *always* zero - the CPU ignores the user provided
911+
* values and silently overwrites them with zero permissions.
912+
*/
913+
if ((secinfo_flags & SGX_SECINFO_PAGE_TYPE_MASK) == SGX_SECINFO_TCS)
914+
prot |= PROT_READ | PROT_WRITE;
915+
916+
/* Calculate maximum of the VM flags for the page. */
917+
encl_page->vm_max_prot_bits = calc_vm_prot_bits(prot, 0);
918+
919+
return encl_page;
920+
}
921+
890922
/**
891923
* sgx_zap_enclave_ptes() - remove PTEs mapping the address from enclave
892924
* @encl: the enclave

arch/x86/kernel/cpu/sgx/encl.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,9 @@ int sgx_encl_get_backing(struct sgx_encl *encl, unsigned long page_index,
112112
void sgx_encl_put_backing(struct sgx_backing *backing);
113113
int sgx_encl_test_and_clear_young(struct mm_struct *mm,
114114
struct sgx_encl_page *page);
115+
struct sgx_encl_page *sgx_encl_page_alloc(struct sgx_encl *encl,
116+
unsigned long offset,
117+
u64 secinfo_flags);
115118
void sgx_zap_enclave_ptes(struct sgx_encl *encl, unsigned long addr);
116119
struct sgx_epc_page *sgx_alloc_va_page(void);
117120
unsigned int sgx_alloc_va_slot(struct sgx_va_page *va_page);

arch/x86/kernel/cpu/sgx/ioctl.c

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -169,38 +169,6 @@ static long sgx_ioc_enclave_create(struct sgx_encl *encl, void __user *arg)
169169
return ret;
170170
}
171171

172-
static struct sgx_encl_page *sgx_encl_page_alloc(struct sgx_encl *encl,
173-
unsigned long offset,
174-
u64 secinfo_flags)
175-
{
176-
struct sgx_encl_page *encl_page;
177-
unsigned long prot;
178-
179-
encl_page = kzalloc(sizeof(*encl_page), GFP_KERNEL);
180-
if (!encl_page)
181-
return ERR_PTR(-ENOMEM);
182-
183-
encl_page->desc = encl->base + offset;
184-
encl_page->encl = encl;
185-
186-
prot = _calc_vm_trans(secinfo_flags, SGX_SECINFO_R, PROT_READ) |
187-
_calc_vm_trans(secinfo_flags, SGX_SECINFO_W, PROT_WRITE) |
188-
_calc_vm_trans(secinfo_flags, SGX_SECINFO_X, PROT_EXEC);
189-
190-
/*
191-
* TCS pages must always RW set for CPU access while the SECINFO
192-
* permissions are *always* zero - the CPU ignores the user provided
193-
* values and silently overwrites them with zero permissions.
194-
*/
195-
if ((secinfo_flags & SGX_SECINFO_PAGE_TYPE_MASK) == SGX_SECINFO_TCS)
196-
prot |= PROT_READ | PROT_WRITE;
197-
198-
/* Calculate maximum of the VM flags for the page. */
199-
encl_page->vm_max_prot_bits = calc_vm_prot_bits(prot, 0);
200-
201-
return encl_page;
202-
}
203-
204172
static int sgx_validate_secinfo(struct sgx_secinfo *secinfo)
205173
{
206174
u64 perm = secinfo->flags & SGX_SECINFO_PERMISSION_MASK;

0 commit comments

Comments
 (0)