| 1 |
# Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 |
# SPDX-License-Identifier: LGPL-2.1-or-later |
| 3 |
|
| 4 |
from ctypes import c_int, c_int64, c_size_t, c_void_p |
| 5 |
import mmap |
| 6 |
import unittest |
| 7 |
|
| 8 |
from _drgn_util.platform import NORMALIZED_MACHINE_NAME |
| 9 |
from drgn.helpers.linux.boot import pgtable_l5_enabled |
| 10 |
from tests.linux_kernel import LinuxKernelTestCase, _c, iter_maps |
| 11 |
|
| 12 |
|
| 13 |
def first_available_slot(size, min_addr): |
| 14 |
for map in iter_maps(): |
| 15 |
if map.start >= min_addr + size: |
| 16 |
break |
| 17 |
elif map.end >= min_addr: |
| 18 |
min_addr = map.end |
| 19 |
return min_addr |
| 20 |
|
| 21 |
|
| 22 |
def can_mmap_high_address(): |
| 23 |
mmap_func = _c.mmap |
| 24 |
mmap_func.argtypes = [c_void_p, c_size_t, c_int, c_int, c_int, c_int64] |
| 25 |
mmap_func.restype = c_void_p |
| 26 |
munmap_func = _c.munmap |
| 27 |
munmap_func.argtypes = [c_void_p, c_size_t] |
| 28 |
hint_addr = first_available_slot(mmap.PAGESIZE, 1 << 48) |
| 29 |
|
| 30 |
ret = mmap_func( |
| 31 |
hint_addr, |
| 32 |
mmap.PAGESIZE, |
| 33 |
mmap.PROT_READ | mmap.PROT_WRITE, |
| 34 |
# Ideally we would use MAP_FIXED, but its value is not exposed by the |
| 35 |
# mmap module, and it varies by architecture. Having identified a free |
| 36 |
# slot in our memory mappings (and hopefully not changing them since |
| 37 |
# then), we can be reasonably confident that we should get the address |
| 38 |
# we hinted anyway. |
| 39 |
mmap.MAP_PRIVATE | mmap.MAP_ANONYMOUS, |
| 40 |
-1, |
| 41 |
0, |
| 42 |
) |
| 43 |
if ret != c_void_p(-1).value: |
| 44 |
munmap_func(ret, mmap.PAGESIZE) |
| 45 |
return ret == hint_addr |
| 46 |
|
| 47 |
|
| 48 |
class TestBoot(LinuxKernelTestCase): |
| 49 |
@unittest.skipUnless(NORMALIZED_MACHINE_NAME == "x86_64", "machine is not x86_64") |
| 50 |
def test_pgtable_l5_enabled(self): |
| 51 |
self.assertEqual(pgtable_l5_enabled(self.prog), can_mmap_high_address()) |