LCOV - code coverage report
Current view: top level - core/test - run-mem_region_release_unused.c (source / functions) Hit Total Coverage
Test: skiboot.info Lines: 79 82 96.3 %
Date: 2024-01-02 21:04:04 Functions: 9 10 90.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                 :         11 : static void *__malloc(size_t size, const char *location __attribute__((unused)))
      15                 :            : {
      16                 :         11 :         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                 :          1 : static void *__zalloc(size_t size, const char *location __attribute__((unused)))
      25                 :            : {
      26                 :          1 :         return calloc(size, 1);
      27                 :            : }
      28                 :            : 
      29                 :         16 : static inline void __free(void *p, const char *location __attribute__((unused)))
      30                 :            : {
      31                 :         16 :         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                 :          1 : bool lock_held_by_me(struct lock *l)
      62                 :            : {
      63                 :          1 :         return l->lock_val;
      64                 :            : }
      65                 :            : 
      66                 :            : #define TEST_HEAP_ORDER 16
      67                 :            : #define TEST_HEAP_SIZE (1ULL << TEST_HEAP_ORDER)
      68                 :            : 
      69                 :          1 : 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                 :          1 :         name = (char*)malloc(sizeof("memory@") + STR_MAX_CHARS(reg[0]));
      76                 :          1 :         assert(name);
      77                 :            : 
      78                 :            :         /* reg contains start and length */
      79                 :          1 :         reg[0] = cpu_to_be64(start);
      80                 :          1 :         reg[1] = cpu_to_be64(len);
      81                 :            : 
      82                 :          1 :         sprintf(name, "memory@%llx", (long long)start);
      83                 :            : 
      84                 :          1 :         mem = dt_new(dt_root, name);
      85                 :          1 :         dt_add_property_string(mem, "device_type", "memory");
      86                 :          1 :         dt_add_property(mem, "reg", reg, sizeof(reg));
      87                 :          1 :         free(name);
      88                 :          1 : }
      89                 :            : 
      90                 :          1 : void add_chip_dev_associativity(struct dt_node *dev __attribute__((unused)))
      91                 :            : {
      92                 :          1 : }
      93                 :            : 
      94                 :          1 : int main(void)
      95                 :            : {
      96                 :            :         uint64_t i;
      97                 :          1 :         struct mem_region *r, *other = NULL;
      98                 :            :         void *other_mem;
      99                 :            :         const char *last;
     100                 :            : 
     101                 :            :         /* Use malloc for the heap, so valgrind can find issues. */
     102                 :          1 :         skiboot_heap.start = (unsigned long)malloc(TEST_HEAP_SIZE);
     103                 :          1 :         skiboot_heap.len = TEST_HEAP_SIZE;
     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 :         other_mem = malloc(1024*1024);
     111                 :          1 :         add_mem_node((unsigned long)other_mem, 1024*1024);
     112                 :            : 
     113                 :            :         /* Now convert. */
     114                 :          1 :         mem_region_init();
     115                 :            : 
     116                 :            :         /* Find our node to allocate from */
     117                 :          7 :         list_for_each(&regions, r, list) {
     118                 :          6 :                 if (region_start(r) == other_mem)
     119                 :          1 :                         other = r;
     120                 :            :         }
     121                 :            :         /* This could happen if skiboot addresses clashed with our alloc. */
     122                 :          1 :         assert(other);
     123                 :          1 :         assert(mem_check(other));
     124                 :            : 
     125                 :            :         /* Allocate 1k from other region. */
     126                 :          1 :         lock(&other->free_list_lock);
     127                 :          1 :         mem_alloc(other, 1024, 1, "1k");
     128                 :          1 :         unlock(&other->free_list_lock);
     129                 :            : 
     130                 :          1 :         mem_region_release_unused();
     131                 :            : 
     132                 :          1 :         assert(mem_check(&skiboot_heap));
     133                 :            : 
     134                 :            :         /* Now we expect it to be split. */
     135                 :          1 :         i = 0;
     136                 :          8 :         list_for_each(&regions, r, list) {
     137                 :          7 :                 assert(mem_check(r));
     138                 :          7 :                 i++;
     139                 :          7 :                 if (r == &skiboot_os_reserve)
     140                 :          1 :                         continue;
     141                 :          6 :                 if (r == &skiboot_code_and_text)
     142                 :          1 :                         continue;
     143                 :          5 :                 if (r == &skiboot_heap)
     144                 :          1 :                         continue;
     145                 :          4 :                 if (r == &skiboot_after_heap)
     146                 :          1 :                         continue;
     147                 :          3 :                 if (r == &skiboot_cpu_stacks)
     148                 :          1 :                         continue;
     149                 :          2 :                 if (r == other) {
     150                 :          1 :                         assert(r->type == REGION_MEMORY);
     151                 :          1 :                         assert(r->len < 1024 * 1024);
     152                 :            :                 } else {
     153                 :          1 :                         assert(r->type == REGION_OS);
     154                 :          1 :                         assert(r->start == other->start + other->len);
     155                 :          1 :                         assert(r->start + r->len == other->start + 1024*1024);
     156                 :            :                 }
     157                 :            :         }
     158                 :          1 :         assert(i == 7);
     159                 :            : 
     160                 :          1 :         last = NULL;
     161                 :          1 :         list_for_each(&regions, r, list) {
     162                 :          1 :                 if (last != r->name &&
     163                 :          1 :                     strncmp(r->name, NODE_REGION_PREFIX,
     164                 :            :                             strlen(NODE_REGION_PREFIX)) == 0) {
     165                 :            :                         /* It's safe to cast away const as this is
     166                 :            :                          * only going to happen in test code */
     167                 :          1 :                         free((void*)r->name);
     168                 :          1 :                         break;
     169                 :            :                 }
     170                 :          0 :                 last = r->name;
     171                 :            :         }
     172                 :            : 
     173                 :          1 :         dt_free(dt_root);
     174                 :          1 :         free((void *)(long)skiboot_heap.start);
     175                 :          1 :         free(other_mem);
     176                 :          1 :         return 0;
     177                 :            : }

Generated by: LCOV version 1.14