1/*
2 * Copyright 2017, Data61
3 * Commonwealth Scientific and Industrial Research Organisation (CSIRO)
4 * ABN 41 687 119 230.
5 *
6 * This software may be distributed and modified according to the terms of
7 * the BSD 2-Clause license. Note that NO WARRANTY is provided.
8 * See "LICENSE_BSD2.txt" for details.
9 *
10 * @TAG(DATA61_BSD)
11 */
12
13#pragma once
14
15#include <stdbool.h>
16#include <stdint.h>
17#include <utils/arith.h>
18
19#include <vspace/arch/page.h>
20
21#define SEL4_NUM_PAGE_SIZES ((int) ARRAY_SIZE(sel4_page_sizes))
22
23static inline bool
24sel4_valid_size_bits(size_t size_bits)
25{
26    for (int i = 0; i < SEL4_NUM_PAGE_SIZES && size_bits >= sel4_page_sizes[i]; i++) {
27        /* sanity check, sel4_page_sizes should be ordered */
28        if (i > 0) {
29            assert(sel4_page_sizes[i - 1] < sel4_page_sizes[i]);
30        }
31        if (size_bits == sel4_page_sizes[i]) {
32            return true;
33        }
34    }
35
36    return false;
37}
38
39static inline int
40sel4_page_size_bits_for_memory_region(size_t size_bytes) {
41    int frame_size_index = 0;
42    /* find the largest reasonable frame size */
43    while (frame_size_index + 1 < SEL4_NUM_PAGE_SIZES) {
44        if (size_bytes >> sel4_page_sizes[frame_size_index + 1] == 0) {
45            break;
46        }
47        frame_size_index++;
48    }
49    return sel4_page_sizes[frame_size_index];
50}
51
52