Path: blob/master/tools/testing/memblock/tests/common.h
26299 views
/* SPDX-License-Identifier: GPL-2.0-or-later */1#ifndef _MEMBLOCK_TEST_H2#define _MEMBLOCK_TEST_H34#include <stdlib.h>5#include <assert.h>6#include <linux/types.h>7#include <linux/seq_file.h>8#include <linux/memblock.h>9#include <linux/sizes.h>10#include <linux/printk.h>11#include <../selftests/kselftest.h>1213#define MEM_SIZE SZ_32K14#define PHYS_MEM_SIZE SZ_16M15#define NUMA_NODES 81617#define INIT_MEMBLOCK_REGIONS 12818#define INIT_MEMBLOCK_RESERVED_REGIONS INIT_MEMBLOCK_REGIONS1920enum test_flags {21/* No special request. */22TEST_F_NONE = 0x0,23/* Perform raw allocations (no zeroing of memory). */24TEST_F_RAW = 0x1,25/* Perform allocations on the exact node specified. */26TEST_F_EXACT = 0x227};2829/**30* ASSERT_EQ():31* Check the condition32* @_expected == @_seen33* If false, print failed test message (if running with --verbose) and then34* assert.35*/36#define ASSERT_EQ(_expected, _seen) do { \37if ((_expected) != (_seen)) \38test_fail(); \39assert((_expected) == (_seen)); \40} while (0)4142#define ASSERT_TRUE(_seen) ASSERT_EQ(true, _seen)43#define ASSERT_FALSE(_seen) ASSERT_EQ(false, _seen)4445/**46* ASSERT_NE():47* Check the condition48* @_expected != @_seen49* If false, print failed test message (if running with --verbose) and then50* assert.51*/52#define ASSERT_NE(_expected, _seen) do { \53if ((_expected) == (_seen)) \54test_fail(); \55assert((_expected) != (_seen)); \56} while (0)5758/**59* ASSERT_LT():60* Check the condition61* @_expected < @_seen62* If false, print failed test message (if running with --verbose) and then63* assert.64*/65#define ASSERT_LT(_expected, _seen) do { \66if ((_expected) >= (_seen)) \67test_fail(); \68assert((_expected) < (_seen)); \69} while (0)7071/**72* ASSERT_LE():73* Check the condition74* @_expected <= @_seen75* If false, print failed test message (if running with --verbose) and then76* assert.77*/78#define ASSERT_LE(_expected, _seen) do { \79if ((_expected) > (_seen)) \80test_fail(); \81assert((_expected) <= (_seen)); \82} while (0)8384/**85* ASSERT_MEM_EQ():86* Check that the first @_size bytes of @_seen are all equal to @_expected.87* If false, print failed test message (if running with --verbose) and then88* assert.89*/90#define ASSERT_MEM_EQ(_seen, _expected, _size) do { \91for (int _i = 0; _i < (_size); _i++) { \92ASSERT_EQ(((char *)_seen)[_i], (_expected)); \93} \94} while (0)9596/**97* ASSERT_MEM_NE():98* Check that none of the first @_size bytes of @_seen are equal to @_expected.99* If false, print failed test message (if running with --verbose) and then100* assert.101*/102#define ASSERT_MEM_NE(_seen, _expected, _size) do { \103for (int _i = 0; _i < (_size); _i++) { \104ASSERT_NE(((char *)_seen)[_i], (_expected)); \105} \106} while (0)107108#define PREFIX_PUSH() prefix_push(__func__)109110/*111* Available memory registered with memblock needs to be valid for allocs112* test to run. This is a convenience wrapper for memory allocated in113* dummy_physical_memory_init() that is later registered with memblock114* in setup_memblock().115*/116struct test_memory {117void *base;118};119120struct region {121phys_addr_t base;122phys_addr_t size;123};124125static inline phys_addr_t __maybe_unused region_end(struct memblock_region *rgn)126{127return rgn->base + rgn->size;128}129130void reset_memblock_regions(void);131void reset_memblock_attributes(void);132void setup_memblock(void);133void setup_numa_memblock(const unsigned int node_fracs[]);134void dummy_physical_memory_init(void);135void dummy_physical_memory_cleanup(void);136phys_addr_t dummy_physical_memory_base(void);137void parse_args(int argc, char **argv);138139void test_fail(void);140void test_pass(void);141void test_print(const char *fmt, ...);142void prefix_reset(void);143void prefix_push(const char *prefix);144void prefix_pop(void);145146static inline void test_pass_pop(void)147{148test_pass();149prefix_pop();150}151152static inline void run_top_down(int (*func)())153{154memblock_set_bottom_up(false);155prefix_push("top-down");156func();157prefix_pop();158}159160static inline void run_bottom_up(int (*func)())161{162memblock_set_bottom_up(true);163prefix_push("bottom-up");164func();165prefix_pop();166}167168static inline void assert_mem_content(void *mem, int size, int flags)169{170if (flags & TEST_F_RAW)171ASSERT_MEM_NE(mem, 0, size);172else173ASSERT_MEM_EQ(mem, 0, size);174}175176#endif177178179