LCOV - code coverage report
Current view: top level - core/test - run-mem_region_release_unused_noalloc.c (source / functions) Hit Total Coverage
Test: skiboot.info Lines: 62 66 93.9 %
Date: 2024-01-02 21:04:04 Functions: 8 10 80.0 %
Branches: 0 0 -

           Branch data     Line data    Source code
       1                 :            : // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
       2                 :            : /*
       3                 :            :  * Copyright 2013-2018 IBM Corp.
       4                 :            :  */
       5                 :            : 
       6                 :            : #include <config.h>
       7                 :            : 
       8                 :            : #define BITS_PER_LONG (sizeof(long) * 8)
       9                 :            : 
      10                 :            : #include "dummy-cpu.h"
      11                 :            : 
      12                 :            : #include <stdlib.h>
      13                 :            : 
      14                 :         19 : static void *__malloc(size_t size, const char *location __attribute__((unused)))
      15                 :            : {
      16                 :         19 :         return malloc(size);
      17                 :            : }
      18                 :            : 
      19                 :          0 : static void *__realloc(void *ptr, size_t size, const char *location __attribute__((unused)))
      20                 :            : {
      21                 :          0 :         return realloc(ptr, size);
      22                 :            : }
      23                 :            : 
      24                 :          2 : static void *__zalloc(size_t size, const char *location __attribute__((unused)))
      25                 :            : {
      26                 :          2 :         return calloc(size, 1);
      27                 :            : }
      28                 :            : 
      29                 :         26 : static inline void __free(void *p, const char *location __attribute__((unused)))
      30                 :            : {
      31                 :         26 :         return free(p);
      32                 :            : }
      33                 :            : 
      34                 :            : #include <skiboot.h>
      35                 :            : 
      36                 :            : /* We need mem_region to accept __location__ */
      37                 :            : #define is_rodata(p) true
      38                 :            : #include "../mem_region.c"
      39                 :            : 
      40                 :            : /* But we need device tree to make copies of names. */
      41                 :            : #undef is_rodata
      42                 :            : #define is_rodata(p) false
      43                 :            : 
      44                 :            : #include "../device.c"
      45                 :            : #include <assert.h>
      46                 :            : #include <stdio.h>
      47                 :            : 
      48                 :            : enum proc_chip_quirks proc_chip_quirks;
      49                 :            : 
      50                 :          4 : void lock_caller(struct lock *l, const char *caller)
      51                 :            : {
      52                 :            :         (void)caller;
      53                 :          4 :         l->lock_val++;
      54                 :          4 : }
      55                 :            : 
      56                 :          4 : void unlock(struct lock *l)
      57                 :            : {
      58                 :          4 :         l->lock_val--;
      59                 :          4 : }
      60                 :            : 
      61                 :          0 : bool lock_held_by_me(struct lock *l)
      62                 :            : {
      63                 :          0 :         return l->lock_val;
      64                 :            : }
      65                 :            : 
      66                 :            : #define TEST_HEAP_ORDER 16
      67                 :            : #define TEST_HEAP_SIZE (1ULL << TEST_HEAP_ORDER)
      68                 :            : 
      69                 :          2 : static void add_mem_node(uint64_t start, uint64_t len)
      70                 :            : {
      71                 :            :         struct dt_node *mem;
      72                 :            :         u64 reg[2];
      73                 :            :         char *name;
      74                 :            : 
      75                 :          2 :         name = (char*)malloc(sizeof("memory@") + STR_MAX_CHARS(reg[0]));
      76                 :          2 :         assert(name);
      77                 :            : 
      78                 :            :         /* reg contains start and length */
      79                 :          2 :         reg[0] = cpu_to_be64(start);
      80                 :          2 :         reg[1] = cpu_to_be64(len);
      81                 :            : 
      82                 :          2 :         sprintf(name, "memory@%llx", (long long)start);
      83                 :            : 
      84                 :          2 :         mem = dt_new(dt_root, name);
      85                 :          2 :         dt_add_property_string(mem, "device_type", "memory");
      86                 :          2 :         dt_add_property(mem, "reg", reg, sizeof(reg));
      87                 :          2 :         free(name);
      88                 :          2 : }
      89                 :            : 
      90                 :          2 : void add_chip_dev_associativity(struct dt_node *dev __attribute__((unused)))
      91                 :            : {
      92                 :          2 : }
      93                 :            : 
      94                 :          1 : int main(void)
      95                 :            : {
      96                 :            :         uint64_t i;
      97                 :            :         struct mem_region *r;
      98                 :            :         const char *last;
      99                 :            : 
     100                 :            :         /* Use malloc for the heap, so valgrind can find issues. */
     101                 :          1 :         skiboot_heap.start = 0;
     102                 :          1 :         skiboot_heap.len = TEST_HEAP_SIZE;
     103                 :          1 :         skiboot_os_reserve.start = 0;
     104                 :          1 :         skiboot_os_reserve.len = 0;
     105                 :            : 
     106                 :          1 :         dt_root = dt_new_root("");
     107                 :          1 :         dt_add_property_cells(dt_root, "#address-cells", 2);
     108                 :          1 :         dt_add_property_cells(dt_root, "#size-cells", 2);
     109                 :            : 
     110                 :          1 :         add_mem_node(0, 0x100000000ULL);
     111                 :          1 :         add_mem_node(0x100000000ULL, 0x100000000ULL);
     112                 :            : 
     113                 :          1 :         mem_region_init();
     114                 :            : 
     115                 :          1 :         mem_region_release_unused();
     116                 :            : 
     117                 :          1 :         assert(mem_check(&skiboot_heap));
     118                 :            : 
     119                 :            :         /* Now we expect it to be split. */
     120                 :          1 :         i = 0;
     121                 :         10 :         list_for_each(&regions, r, list) {
     122                 :          9 :                 assert(mem_check(r));
     123                 :          9 :                 i++;
     124                 :          9 :                 if (r == &skiboot_os_reserve)
     125                 :          1 :                         continue;
     126                 :          8 :                 if (r == &skiboot_code_and_text)
     127                 :          1 :                         continue;
     128                 :          7 :                 if (r == &skiboot_heap)
     129                 :          1 :                         continue;
     130                 :          6 :                 if (r == &skiboot_after_heap)
     131                 :          1 :                         continue;
     132                 :          5 :                 if (r == &skiboot_cpu_stacks)
     133                 :          1 :                         continue;
     134                 :            : 
     135                 :            :                 /* the memory nodes should all be available to the OS now */
     136                 :          4 :                 assert(r->type == REGION_OS);
     137                 :            :         }
     138                 :          1 :         assert(i == 9);
     139                 :            : 
     140                 :          1 :         last = NULL;
     141                 :         10 :         list_for_each(&regions, r, list) {
     142                 :          9 :                 if (last != r->name &&
     143                 :          7 :                     strncmp(r->name, NODE_REGION_PREFIX,
     144                 :            :                             strlen(NODE_REGION_PREFIX)) == 0) {
     145                 :            :                         /* It's safe to cast away the const as
     146                 :            :                          * this never happens at runtime,
     147                 :            :                          * only in test and only for valgrind
     148                 :            :                          */
     149                 :          2 :                         free((void*)r->name);
     150                 :          2 :                         last = r->name;
     151                 :            :                 }
     152                 :            :         }
     153                 :            : 
     154                 :          1 :         dt_free(dt_root);
     155                 :          1 :         return 0;
     156                 :            : }

Generated by: LCOV version 1.14