LCOV - code coverage report
Current view: top level - core/test - run-mem_region_init.c (source / functions) Hit Total Coverage
Test: skiboot.info Lines: 80 80 100.0 %
Date: 2024-01-02 21:04:04 Functions: 9 9 100.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                 :            : /* Use these before we undefine them below. */
      15                 :          1 : static inline void *real_malloc(size_t size)
      16                 :            : {
      17                 :          1 :         return malloc(size);
      18                 :            : }
      19                 :            : 
      20                 :          1 : static inline void real_free(void *p)
      21                 :            : {
      22                 :          1 :         return free(p);
      23                 :            : }
      24                 :            : 
      25                 :            : #include "../malloc.c"
      26                 :            : 
      27                 :            : #include <skiboot.h>
      28                 :            : /* We need mem_region to accept __location__ */
      29                 :            : #define is_rodata(p) true
      30                 :            : #include "../mem_region.c"
      31                 :            : 
      32                 :            : /* But we need device tree to make copies of names. */
      33                 :            : #undef is_rodata
      34                 :            : #define is_rodata(p) false
      35                 :            : 
      36                 :          9 : static inline char *skiboot_strdup(const char *str)
      37                 :            : {
      38                 :          9 :         char *ret = __malloc(strlen(str) + 1, "");
      39                 :          9 :         return memcpy(ret, str, strlen(str) + 1);
      40                 :            : }
      41                 :            : #undef strdup
      42                 :            : #define strdup skiboot_strdup
      43                 :            : 
      44                 :            : #include "../device.c"
      45                 :            : 
      46                 :            : #include <skiboot.h>
      47                 :            : 
      48                 :            : #include <assert.h>
      49                 :            : #include <stdio.h>
      50                 :            : 
      51                 :            : enum proc_chip_quirks proc_chip_quirks;
      52                 :            : 
      53                 :         65 : void lock_caller(struct lock *l, const char *caller)
      54                 :            : {
      55                 :            :         (void)caller;
      56                 :         65 :         assert(!l->lock_val);
      57                 :         65 :         l->lock_val = 1;
      58                 :         65 : }
      59                 :            : 
      60                 :         65 : void unlock(struct lock *l)
      61                 :            : {
      62                 :         65 :         assert(l->lock_val);
      63                 :         65 :         l->lock_val = 0;
      64                 :         65 : }
      65                 :            : 
      66                 :         62 : bool lock_held_by_me(struct lock *l)
      67                 :            : {
      68                 :         62 :         return l->lock_val;
      69                 :            : }
      70                 :            : 
      71                 :            : /* We actually need a lot of room for the bitmaps! */
      72                 :            : #define TEST_HEAP_ORDER 27
      73                 :            : #define TEST_HEAP_SIZE (1ULL << TEST_HEAP_ORDER)
      74                 :            : 
      75                 :          2 : static void add_mem_node(uint64_t start, uint64_t len)
      76                 :            : {
      77                 :            :         struct dt_node *mem;
      78                 :            :         u64 reg[2];
      79                 :          2 :         char *name= (char*)malloc(sizeof("memory@") + STR_MAX_CHARS(reg[0]));
      80                 :            : 
      81                 :          2 :         assert(name);
      82                 :            : 
      83                 :            :         /* reg contains start and length */
      84                 :          2 :         reg[0] = cpu_to_be64(start);
      85                 :          2 :         reg[1] = cpu_to_be64(len);
      86                 :            : 
      87                 :          2 :         sprintf(name, "memory@%llx", (unsigned long long)start);
      88                 :            : 
      89                 :          2 :         mem = dt_new(dt_root, name);
      90                 :          2 :         assert(mem);
      91                 :          2 :         dt_add_property_string(mem, "device_type", "memory");
      92                 :          2 :         dt_add_property(mem, "reg", reg, sizeof(reg));
      93                 :          2 :         free(name);
      94                 :          2 : }
      95                 :            : 
      96                 :          2 : void add_chip_dev_associativity(struct dt_node *dev __attribute__((unused)))
      97                 :            : {
      98                 :          2 : }
      99                 :            : 
     100                 :          1 : int main(void)
     101                 :            : {
     102                 :            :         uint64_t end;
     103                 :            :         int builtins;
     104                 :            :         struct mem_region *r;
     105                 :          1 :         char *heap = real_malloc(TEST_HEAP_SIZE);
     106                 :            : 
     107                 :            :         /* Use malloc for the heap, so valgrind can find issues. */
     108                 :          1 :         skiboot_heap.start = (unsigned long)heap;
     109                 :          1 :         skiboot_heap.len = TEST_HEAP_SIZE;
     110                 :          1 :         skiboot_os_reserve.len = 16384;
     111                 :            : 
     112                 :          1 :         dt_root = dt_new_root("");
     113                 :          1 :         dt_add_property_cells(dt_root, "#address-cells", 2);
     114                 :          1 :         dt_add_property_cells(dt_root, "#size-cells", 2);
     115                 :            : 
     116                 :            :         /* Make sure we overlap the heap, at least. */
     117                 :          1 :         add_mem_node(0, (uint64_t)(heap + 0x100000000ULL));
     118                 :          1 :         add_mem_node((uint64_t)heap+0x100000000ULL , 0x100000000ULL);
     119                 :          1 :         end = (uint64_t)(heap+ 0x100000000ULL + 0x100000000ULL);
     120                 :            : 
     121                 :            :         /* Now convert. */
     122                 :          1 :         mem_region_init();
     123                 :          1 :         mem_dump_allocs();
     124                 :          1 :         assert(mem_check(&skiboot_heap));
     125                 :            : 
     126                 :          1 :         builtins = 0;
     127                 :         11 :         list_for_each(&regions, r, list) {
     128                 :            :                 /* Regions must not overlap. */
     129                 :         10 :                 struct mem_region *r2, *pre = NULL, *post = NULL;
     130                 :        110 :                 list_for_each(&regions, r2, list) {
     131                 :        100 :                         if (r == r2)
     132                 :         10 :                                 continue;
     133                 :         90 :                         assert(!overlaps(r, r2));
     134                 :            :                 }
     135                 :            : 
     136                 :            :                 /* But should have exact neighbours. */
     137                 :        110 :                 list_for_each(&regions, r2, list) {
     138                 :        100 :                         if (r == r2)
     139                 :         10 :                                 continue;
     140                 :         90 :                         if (r2->start == r->start + r->len)
     141                 :          9 :                                 post = r2;
     142                 :         90 :                         if (r2->start + r2->len == r->start)
     143                 :          9 :                                 pre = r2;
     144                 :            :                 }
     145                 :         10 :                 assert(r->start == 0 || pre);
     146                 :         10 :                 assert(r->start + r->len == end || post);
     147                 :            : 
     148                 :         10 :                 if (r == &skiboot_code_and_text ||
     149                 :          8 :                     r == &skiboot_heap ||
     150                 :          7 :                     r == &skiboot_after_heap ||
     151                 :          6 :                     r == &skiboot_cpu_stacks ||
     152                 :            :                     r == &skiboot_os_reserve)
     153                 :          5 :                         builtins++;
     154                 :            :                 else
     155                 :          5 :                         assert(r->type == REGION_MEMORY);
     156                 :         10 :                 assert(mem_check(r));
     157                 :            :         }
     158                 :          1 :         assert(builtins == 5);
     159                 :            : 
     160                 :          1 :         dt_free(dt_root);
     161                 :            : 
     162                 :         11 :         while ((r = list_pop(&regions, struct mem_region, list)) != NULL) {
     163                 :         10 :                 if (r != &skiboot_code_and_text &&
     164                 :          8 :                     r != &skiboot_heap &&
     165                 :          7 :                     r != &skiboot_after_heap &&
     166                 :          6 :                     r != &skiboot_os_reserve &&
     167                 :            :                     r != &skiboot_cpu_stacks) {
     168                 :          5 :                         free(r);
     169                 :            :                 }
     170                 :         10 :                 assert(mem_check(&skiboot_heap));
     171                 :            :         }
     172                 :          1 :         assert(skiboot_heap.free_list_lock.lock_val == 0);
     173                 :          1 :         real_free(heap);
     174                 :          1 :         return 0;
     175                 :            : }

Generated by: LCOV version 1.14