Linux-Sgx Archive on lore.kernel.org
 help / color / Atom feed
From: Sean Christopherson <sean.j.christopherson@intel.com>
To: Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com>
Cc: linux-sgx@vger.kernel.org
Subject: [PATCH for_v22] selftests/x86/sgx: Ensure SECS base (ELRANGE) is naturally aligned
Date: Fri,  9 Aug 2019 17:30:51 -0700
Message-ID: <20190810003051.15712-1-sean.j.christopherson@intel.com> (raw)

Manually align ELRANGE now that the kernel doesn't automatically do so
for SGX mappings.  To guarantee mmap() returns a region that can be
naturally aligned, temporarily map 2x the enclave size and do fancy
arithmetic to naturally align the base.

Signed-off-by: Sean Christopherson <sean.j.christopherson@intel.com>
---
 tools/testing/selftests/x86/sgx/main.c | 19 ++++++++++++++-----
 1 file changed, 14 insertions(+), 5 deletions(-)

diff --git a/tools/testing/selftests/x86/sgx/main.c b/tools/testing/selftests/x86/sgx/main.c
index effcdb3380ad..5bbc60cf7f89 100644
--- a/tools/testing/selftests/x86/sgx/main.c
+++ b/tools/testing/selftests/x86/sgx/main.c
@@ -130,7 +130,8 @@ static bool encl_create(int dev_fd, unsigned long bin_size,
 			struct sgx_secs *secs)
 {
 	struct sgx_enclave_create ioc;
-	void *base;
+	uint64_t base;
+	void *basep;
 	int rc;
 
 	memset(secs, 0, sizeof(*secs));
@@ -141,19 +142,27 @@ static bool encl_create(int dev_fd, unsigned long bin_size,
 	for (secs->size = 4096; secs->size < bin_size; )
 		secs->size <<= 1;
 
-	base = mmap(NULL, secs->size, PROT_NONE, MAP_SHARED, dev_fd, 0);
-	if (base == MAP_FAILED) {
+	basep = mmap(NULL, secs->size * 2, PROT_NONE, MAP_SHARED, dev_fd, 0);
+	if (basep == MAP_FAILED) {
 		perror("mmap");
 		return false;
 	}
+	base = (uint64_t)basep;
 
-	secs->base = (uint64_t)base;
+	secs->base = base + secs->size - (base % secs->size);
+
+	if (secs->base != base)
+		munmap(basep, secs->base - base);
+
+	if (secs->base - secs->size != base)
+		munmap((void *)(secs->base + secs->size),
+		       base + secs->size - secs->base);
 
 	ioc.src = (unsigned long)secs;
 	rc = ioctl(dev_fd, SGX_IOC_ENCLAVE_CREATE, &ioc);
 	if (rc) {
 		fprintf(stderr, "ECREATE failed rc=%d, err=%d.\n", rc, errno);
-		munmap(base, secs->size);
+		munmap((void *)secs->base, secs->size);
 		return false;
 	}
 
-- 
2.22.0


             reply index

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-08-10  0:30 Sean Christopherson [this message]
2019-08-10  0:39 ` Jethro Beekman
2019-08-10 11:23   ` Jarkko Sakkinen
2019-08-15 12:26   ` Jarkko Sakkinen
2019-08-10 11:22 ` Jarkko Sakkinen

Reply instructions:

You may reply publically to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20190810003051.15712-1-sean.j.christopherson@intel.com \
    --to=sean.j.christopherson@intel.com \
    --cc=jarkko.sakkinen@linux.intel.com \
    --cc=linux-sgx@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

Linux-Sgx Archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/linux-sgx/0 linux-sgx/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 linux-sgx linux-sgx/ https://lore.kernel.org/linux-sgx \
		linux-sgx@vger.kernel.org linux-sgx@archiver.kernel.org
	public-inbox-index linux-sgx


Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/org.kernel.vger.linux-sgx


AGPL code for this site: git clone https://public-inbox.org/ public-inbox