72332c
From c37583942c14d3574efbb42e39798bd21e65cfc3 Mon Sep 17 00:00:00 2001
72332c
From: Dmitry Safonov <dima@arista.com>
72332c
Date: Thu, 10 May 2018 19:14:48 +0100
72332c
Subject: [PATCH 8/8] ppc64/aarch64: Dynamically define PAGE_SIZE
72332c
72332c
On ppc64/aarch64 Linux can be set to use Large pages, so the PAGE_SIZE
72332c
isn't build-time constant anymore. Define it through _SC_PAGESIZE.
72332c
72332c
There are different sizes for a page on ppc64:
72332c
: #if defined(CONFIG_PPC_256K_PAGES)
72332c
: #define PAGE_SHIFT              18
72332c
: #elif defined(CONFIG_PPC_64K_PAGES)
72332c
: #define PAGE_SHIFT              16
72332c
: #elif defined(CONFIG_PPC_16K_PAGES)
72332c
: #define PAGE_SHIFT              14
72332c
: #else
72332c
: #define PAGE_SHIFT              12
72332c
: #endif
72332c
72332c
And on aarch64 there are default sizes and possibly someone can set his
72332c
own PAGE_SHIFT:
72332c
: config ARM64_PAGE_SHIFT
72332c
:         int
72332c
:         default 16 if ARM64_64K_PAGES
72332c
:         default 14 if ARM64_16K_PAGES
72332c
:         default 12
72332c
72332c
On the downside - each time we need PAGE_SIZE, we're doing libc
72332c
function call on aarch64/ppc64.
72332c
72332c
Fixes: #415
72332c
72332c
Tested-by: Adrian Reber <areber@redhat.com>
72332c
Signed-off-by: Dmitry Safonov <dima@arista.com>
72332c
Signed-off-by: Andrei Vagin <avagin@virtuozzo.com>
72332c
---
72332c
 criu/arch/aarch64/crtools.c            |  3 ++
72332c
 criu/arch/ppc64/crtools.c              |  3 ++
72332c
 include/common/arch/aarch64/asm/page.h | 42 +++++++++++++++++---------
72332c
 include/common/arch/ppc64/asm/page.h   | 40 +++++++++++++++---------
72332c
 4 files changed, 59 insertions(+), 29 deletions(-)
72332c
72332c
diff --git a/criu/arch/aarch64/crtools.c b/criu/arch/aarch64/crtools.c
72332c
index f98743a23..b2ef1c312 100644
72332c
--- a/criu/arch/aarch64/crtools.c
72332c
+++ b/criu/arch/aarch64/crtools.c
72332c
@@ -21,6 +21,9 @@
72332c
 #include "restorer.h"
72332c
 #include <compel/compel.h>
72332c
 
72332c
+unsigned __page_size = 0;
72332c
+unsigned __page_shift = 0;
72332c
+
72332c
 #define assign_reg(dst, src, e)		dst->e = (__typeof__(dst->e))(src)->e
72332c
 
72332c
 int save_task_regs(void *x, user_regs_struct_t *regs, user_fpregs_struct_t *fpsimd)
72332c
diff --git a/criu/arch/ppc64/crtools.c b/criu/arch/ppc64/crtools.c
72332c
index 5a5966ad4..077c243b2 100644
72332c
--- a/criu/arch/ppc64/crtools.c
72332c
+++ b/criu/arch/ppc64/crtools.c
72332c
@@ -23,6 +23,9 @@
72332c
 #include "images/core.pb-c.h"
72332c
 #include "images/creds.pb-c.h"
72332c
 
72332c
+unsigned __page_size = 0;
72332c
+unsigned __page_shift = 0;
72332c
+
72332c
 static UserPpc64FpstateEntry *copy_fp_regs(uint64_t *fpregs)
72332c
 {
72332c
 	UserPpc64FpstateEntry *fpe;
72332c
diff --git a/include/common/arch/aarch64/asm/page.h b/include/common/arch/aarch64/asm/page.h
72332c
index 4126c8474..bd8fe8f71 100644
72332c
--- a/include/common/arch/aarch64/asm/page.h
72332c
+++ b/include/common/arch/aarch64/asm/page.h
72332c
@@ -4,22 +4,36 @@
72332c
 #define ARCH_HAS_LONG_PAGES
72332c
 
72332c
 #ifndef CR_NOGLIBC
72332c
-#include <unistd.h>
72332c
-
72332c
-#ifndef PAGE_SHIFT
72332c
-# define PAGE_SHIFT	12
72332c
-#endif
72332c
-
72332c
-#ifndef PAGE_SIZE
72332c
-# define PAGE_SIZE	(1UL << PAGE_SHIFT)
72332c
-#endif
72332c
-
72332c
-#ifndef PAGE_MASK
72332c
-# define PAGE_MASK	(~(PAGE_SIZE - 1))
72332c
-#endif
72332c
+#include <string.h> /* ffsl() */
72332c
+#include <unistd.h> /* _SC_PAGESIZE */
72332c
+
72332c
+extern unsigned __page_size;
72332c
+extern unsigned __page_shift;
72332c
+
72332c
+static inline unsigned page_size(void)
72332c
+{
72332c
+	if (!__page_size)
72332c
+		__page_size = sysconf(_SC_PAGESIZE);
72332c
+	return __page_size;
72332c
+}
72332c
+
72332c
+static inline unsigned page_shift(void)
72332c
+{
72332c
+	if (!__page_shift)
72332c
+		__page_shift = (ffsl(page_size()) - 1);
72332c
+	return __page_shift;
72332c
+}
72332c
+
72332c
+/*
72332c
+ * Don't add ifdefs for PAGE_SIZE: if any header defines it as a constant
72332c
+ * on aarch64, then we need refrain using PAGE_SIZE in criu and use
72332c
+ * page_size() across sources (as it may differ on aarch64).
72332c
+ */
72332c
+#define PAGE_SIZE	page_size()
72332c
+#define PAGE_MASK	(~(PAGE_SIZE - 1))
72332c
+#define PAGE_SHIFT	page_shift()
72332c
 
72332c
 #define PAGE_PFN(addr)	((addr) / PAGE_SIZE)
72332c
-#define page_size()	sysconf(_SC_PAGESIZE)
72332c
 
72332c
 #else /* CR_NOGLIBC */
72332c
 
72332c
diff --git a/include/common/arch/ppc64/asm/page.h b/include/common/arch/ppc64/asm/page.h
72332c
index a95af55ef..5107cb8e0 100644
72332c
--- a/include/common/arch/ppc64/asm/page.h
72332c
+++ b/include/common/arch/ppc64/asm/page.h
72332c
@@ -4,26 +4,36 @@
72332c
 #define ARCH_HAS_LONG_PAGES
72332c
 
72332c
 #ifndef CR_NOGLIBC
72332c
-#include <unistd.h>
72332c
+#include <string.h> /* ffsl() */
72332c
+#include <unistd.h> /* _SC_PAGESIZE */
72332c
+
72332c
+extern unsigned __page_size;
72332c
+extern unsigned __page_shift;
72332c
+
72332c
+static inline unsigned page_size(void)
72332c
+{
72332c
+	if (!__page_size)
72332c
+		__page_size = sysconf(_SC_PAGESIZE);
72332c
+	return __page_size;
72332c
+}
72332c
+
72332c
+static inline unsigned page_shift(void)
72332c
+{
72332c
+	if (!__page_shift)
72332c
+		__page_shift = (ffsl(page_size()) - 1);
72332c
+	return __page_shift;
72332c
+}
72332c
 
72332c
 /*
72332c
- * Default config for Pseries is to use 64K pages.
72332c
- * See kernel file arch/powerpc/configs/pseries_*defconfig
72332c
+ * Don't add ifdefs for PAGE_SIZE: if any header defines it as a constant
72332c
+ * on ppc64, then we need refrain using PAGE_SIZE in criu and use
72332c
+ * page_size() across sources (as it may differ on ppc64).
72332c
  */
72332c
-#ifndef PAGE_SHIFT
72332c
-# define PAGE_SHIFT	16
72332c
-#endif
72332c
-
72332c
-#ifndef PAGE_SIZE
72332c
-# define PAGE_SIZE	(1UL << PAGE_SHIFT)
72332c
-#endif
72332c
-
72332c
-#ifndef PAGE_MASK
72332c
-# define PAGE_MASK	(~(PAGE_SIZE - 1))
72332c
-#endif
72332c
+#define PAGE_SIZE	page_size()
72332c
+#define PAGE_MASK	(~(PAGE_SIZE - 1))
72332c
+#define PAGE_SHIFT	page_shift()
72332c
 
72332c
 #define PAGE_PFN(addr)	((addr) / PAGE_SIZE)
72332c
-#define page_size()	sysconf(_SC_PAGESIZE)
72332c
 
72332c
 #else /* CR_NOGLIBC */
72332c
 
72332c
-- 
72332c
2.17.0
72332c