*     gcc -m32 -o protection_keys_32 -O2 -g -std=gnu99 -pthread -Wall protection_keys.c -lrt -ldl -lm
  */
 #define _GNU_SOURCE
+#define __SANE_USERSPACE_TYPES__
 #include <errno.h>
 #include <linux/futex.h>
 #include <sys/time.h>
 int iteration_nr = 1;
 int test_nr;
 
-unsigned int shadow_pkey_reg;
+u64 shadow_pkey_reg;
 int dprint_in_signal;
 char dprint_in_signal_buffer[DPRINT_IN_SIGNAL_BUF_SIZE];
 
 
        for (i = 0; i < len_bytes; i += sizeof(u64)) {
                u64 *ptr = (u64 *)(c + i);
-               dprintf1("dump[%03d][@%p]: %016jx\n", i, ptr, *ptr);
+               dprintf1("dump[%03d][@%p]: %016llx\n", i, ptr, *ptr);
        }
 }
 
 
        dprint_in_signal = 1;
        dprintf1(">>>>===============SIGSEGV============================\n");
-       dprintf1("%s()::%d, pkey_reg: 0x%x shadow: %x\n", __func__, __LINE__,
+       dprintf1("%s()::%d, pkey_reg: 0x%016llx shadow: %016llx\n",
+                       __func__, __LINE__,
                        __read_pkey_reg(), shadow_pkey_reg);
 
        trapno = uctxt->uc_mcontext.gregs[REG_TRAPNO];
        fpregset = uctxt->uc_mcontext.fpregs;
        fpregs = (void *)fpregset;
 
-       dprintf2("%s() trapno: %d ip: 0x%lx info->si_code: %s/%d\n", __func__,
-                       trapno, ip, si_code_str(si->si_code), si->si_code);
+       dprintf2("%s() trapno: %d ip: 0x%016lx info->si_code: %s/%d\n",
+                       __func__, trapno, ip, si_code_str(si->si_code),
+                       si->si_code);
 #ifdef __i386__
        /*
         * 32-bit has some extra padding so that userspace can tell whether
         * need __read_pkey_reg() version so we do not do shadow_pkey_reg
         * checking
         */
-       dprintf1("signal pkey_reg from  pkey_reg: %08x\n", __read_pkey_reg());
-       dprintf1("pkey from siginfo: %jx\n", siginfo_pkey);
+       dprintf1("signal pkey_reg from  pkey_reg: %016llx\n",
+                       __read_pkey_reg());
+       dprintf1("pkey from siginfo: %016llx\n", siginfo_pkey);
        *(u64 *)pkey_reg_ptr = 0x00000000;
        dprintf1("WARNING: set PKEY_REG=0 to allow faulting instruction to continue\n");
        pkey_faults++;
 static u32 hw_pkey_get(int pkey, unsigned long flags)
 {
        u32 mask = (PKEY_DISABLE_ACCESS|PKEY_DISABLE_WRITE);
-       u32 pkey_reg = __read_pkey_reg();
-       u32 shifted_pkey_reg;
+       u64 pkey_reg = __read_pkey_reg();
+       u64 shifted_pkey_reg;
        u32 masked_pkey_reg;
 
        dprintf1("%s(pkey=%d, flags=%lx) = %x / %d\n",
                        __func__, pkey, flags, 0, 0);
-       dprintf2("%s() raw pkey_reg: %x\n", __func__, pkey_reg);
+       dprintf2("%s() raw pkey_reg: %016llx\n", __func__, pkey_reg);
 
        shifted_pkey_reg = (pkey_reg >> (pkey * PKEY_BITS_PER_PKEY));
-       dprintf2("%s() shifted_pkey_reg: %x\n", __func__, shifted_pkey_reg);
+       dprintf2("%s() shifted_pkey_reg: %016llx\n", __func__,
+                       shifted_pkey_reg);
        masked_pkey_reg = shifted_pkey_reg & mask;
        dprintf2("%s() masked  pkey_reg: %x\n", __func__, masked_pkey_reg);
        /*
 static int hw_pkey_set(int pkey, unsigned long rights, unsigned long flags)
 {
        u32 mask = (PKEY_DISABLE_ACCESS|PKEY_DISABLE_WRITE);
-       u32 old_pkey_reg = __read_pkey_reg();
-       u32 new_pkey_reg;
+       u64 old_pkey_reg = __read_pkey_reg();
+       u64 new_pkey_reg;
 
        /* make sure that 'rights' only contains the bits we expect: */
        assert(!(rights & ~mask));
        __write_pkey_reg(new_pkey_reg);
 
        dprintf3("%s(pkey=%d, rights=%lx, flags=%lx) = %x"
-               " pkey_reg now: %x old_pkey_reg: %x\n",
+               " pkey_reg now: %016llx old_pkey_reg: %016llx\n",
                __func__, pkey, rights, flags, 0, __read_pkey_reg(),
                old_pkey_reg);
        return 0;
        unsigned long syscall_flags = 0;
        int ret;
        int pkey_rights;
-       u32 orig_pkey_reg = read_pkey_reg();
+       u64 orig_pkey_reg = read_pkey_reg();
 
        dprintf1("START->%s(%d, 0x%x)\n", __func__,
                pkey, flags);
 
        dprintf1("%s(%d) hw_pkey_get(%d): %x\n", __func__,
                        pkey, pkey, pkey_rights);
+
        pkey_assert(pkey_rights >= 0);
 
        pkey_rights |= flags;
        assert(!ret);
        /* pkey_reg and flags have the same format */
        shadow_pkey_reg |= flags << (pkey * 2);
-       dprintf1("%s(%d) shadow: 0x%x\n", __func__, pkey, shadow_pkey_reg);
+       dprintf1("%s(%d) shadow: 0x%016llx\n",
+               __func__, pkey, shadow_pkey_reg);
 
        pkey_assert(ret >= 0);
 
        dprintf1("%s(%d) hw_pkey_get(%d): %x\n", __func__,
                        pkey, pkey, pkey_rights);
 
-       dprintf1("%s(%d) pkey_reg: 0x%x\n", __func__, pkey, read_pkey_reg());
+       dprintf1("%s(%d) pkey_reg: 0x%016llx\n",
+               __func__, pkey, read_pkey_reg());
        if (flags)
                pkey_assert(read_pkey_reg() > orig_pkey_reg);
        dprintf1("END<---%s(%d, 0x%x)\n", __func__,
        unsigned long syscall_flags = 0;
        int ret;
        int pkey_rights = hw_pkey_get(pkey, syscall_flags);
-       u32 orig_pkey_reg = read_pkey_reg();
+       u64 orig_pkey_reg = read_pkey_reg();
 
        pkey_assert(flags & (PKEY_DISABLE_ACCESS | PKEY_DISABLE_WRITE));
 
        dprintf1("%s(%d) hw_pkey_get(%d): %x\n", __func__,
                        pkey, pkey, pkey_rights);
 
-       dprintf1("%s(%d) pkey_reg: 0x%x\n", __func__, pkey, read_pkey_reg());
+       dprintf1("%s(%d) pkey_reg: 0x%016llx\n", __func__,
+                       pkey, read_pkey_reg());
        if (flags)
                assert(read_pkey_reg() > orig_pkey_reg);
 }
        int ret;
        unsigned long init_val = 0x0;
 
-       dprintf1("%s()::%d, pkey_reg: 0x%x shadow: %x\n", __func__,
-                       __LINE__, __read_pkey_reg(), shadow_pkey_reg);
+       dprintf1("%s()::%d, pkey_reg: 0x%016llx shadow: %016llx\n",
+                       __func__, __LINE__, __read_pkey_reg(), shadow_pkey_reg);
        ret = sys_pkey_alloc(0, init_val);
        /*
         * pkey_alloc() sets PKEY register, so we need to reflect it in
         * shadow_pkey_reg:
         */
-       dprintf4("%s()::%d, ret: %d pkey_reg: 0x%x shadow: 0x%x\n",
+       dprintf4("%s()::%d, ret: %d pkey_reg: 0x%016llx"
+                       " shadow: 0x%016llx\n",
                        __func__, __LINE__, ret, __read_pkey_reg(),
                        shadow_pkey_reg);
        if (ret) {
                /* clear both the bits: */
                shadow_pkey_reg &= ~(0x3      << (ret * 2));
-               dprintf4("%s()::%d, ret: %d pkey_reg: 0x%x shadow: 0x%x\n",
+               dprintf4("%s()::%d, ret: %d pkey_reg: 0x%016llx"
+                               " shadow: 0x%016llx\n",
                                __func__,
                                __LINE__, ret, __read_pkey_reg(),
                                shadow_pkey_reg);
                 */
                shadow_pkey_reg |=  (init_val << (ret * 2));
        }
-       dprintf4("%s()::%d, ret: %d pkey_reg: 0x%x shadow: 0x%x\n",
+       dprintf4("%s()::%d, ret: %d pkey_reg: 0x%016llx"
+                       " shadow: 0x%016llx\n",
                        __func__, __LINE__, ret, __read_pkey_reg(),
                        shadow_pkey_reg);
        dprintf1("%s()::%d errno: %d\n", __func__, __LINE__, errno);
        /* for shadow checking: */
        read_pkey_reg();
-       dprintf4("%s()::%d, ret: %d pkey_reg: 0x%x shadow: 0x%x\n",
+       dprintf4("%s()::%d, ret: %d pkey_reg: 0x%016llx"
+                " shadow: 0x%016llx\n",
                __func__, __LINE__, ret, __read_pkey_reg(),
                shadow_pkey_reg);
        return ret;
                free_ret = sys_pkey_free(alloced_pkeys[i]);
                pkey_assert(!free_ret);
        }
-       dprintf1("%s()::%d, ret: %d pkey_reg: 0x%x shadow: 0x%x\n", __func__,
+       dprintf1("%s()::%d, ret: %d pkey_reg: 0x%016llx"
+                        " shadow: 0x%016llx\n", __func__,
                        __LINE__, ret, __read_pkey_reg(), shadow_pkey_reg);
        return ret;
 }
                if (nr_iterations-- < 0)
                        break;
 
-               dprintf1("%s()::%d, ret: %d pkey_reg: 0x%x shadow: 0x%x\n",
+               dprintf1("%s()::%d, ret: %d pkey_reg: 0x%016llx"
+                       " shadow: 0x%016llx\n",
                        __func__, __LINE__, ret, __read_pkey_reg(),
                        shadow_pkey_reg);
                sys_pkey_free(rpkey);
-               dprintf1("%s()::%d, ret: %d pkey_reg: 0x%x shadow: 0x%x\n",
+               dprintf1("%s()::%d, ret: %d pkey_reg: 0x%016llx"
+                       " shadow: 0x%016llx\n",
                        __func__, __LINE__, ret, __read_pkey_reg(),
                        shadow_pkey_reg);
        }
        dprintf1("mprotect_pkey(%p, %zx, prot=0x%lx, pkey=%ld) ret: %d\n",
                        ptr, size, orig_prot, pkey, ret);
        pkey_assert(!ret);
-       dprintf1("%s()::%d, ret: %d pkey_reg: 0x%x shadow: 0x%x\n", __func__,
+       dprintf1("%s()::%d, ret: %d pkey_reg: 0x%016llx"
+                       " shadow: 0x%016llx\n", __func__,
                        __LINE__, ret, __read_pkey_reg(), shadow_pkey_reg);
        return ret;
 }
                pkey_assert(0);
 
        __write_pkey_reg(shadow_pkey_reg);
-       dprintf1("%s() set pkey_reg=%x to restore state after signal "
+       dprintf1("%s() set pkey_reg=%016llx to restore state after signal "
                       "nuked it\n", __func__, shadow_pkey_reg);
        last_pkey_faults = pkey_faults;
        last_si_pkey = -1;
                int new_pkey;
                dprintf1("%s() alloc loop: %d\n", __func__, i);
                new_pkey = alloc_pkey();
-               dprintf4("%s()::%d, err: %d pkey_reg: 0x%x shadow: 0x%x\n",
+               dprintf4("%s()::%d, err: %d pkey_reg: 0x%016llx"
+                               " shadow: 0x%016llx\n",
                                __func__, __LINE__, err, __read_pkey_reg(),
                                shadow_pkey_reg);
                read_pkey_reg(); /* for shadow checking */
        pkey_assert(!ret);
        pkey_access_deny(pkey);
 
-       dprintf2("pkey_reg: %x\n", read_pkey_reg());
+       dprintf2("pkey_reg: %016llx\n", read_pkey_reg());
 
        /*
         * Make sure this is an *instruction* fault
        ret = mprotect(p1, PAGE_SIZE, PROT_EXEC);
        pkey_assert(!ret);
 
-       dprintf2("pkru: %x\n", read_pkey_reg());
+       dprintf2("pkey_reg: %016llx\n", read_pkey_reg());
 
        /* Make sure this is an *instruction* fault */
        madvise(p1, PAGE_SIZE, MADV_DONTNEED);
        }
 
        pkey_setup_shadow();
-       printf("startup pkey_reg: %x\n", read_pkey_reg());
+       printf("startup pkey_reg: %016llx\n", read_pkey_reg());
        setup_hugetlbfs();
 
        while (nr_iterations-- > 0)