Lee Kai Xuan / mbed-os

Fork of mbed-os by erkin yucel

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers unsupported_page_allocator.c Source File

unsupported_page_allocator.c

00001 /*
00002  * Copyright (c) 2015-2016, ARM Limited, All Rights Reserved
00003  * SPDX-License-Identifier: Apache-2.0
00004  *
00005  * Licensed under the Apache License, Version 2.0 (the "License"); you may
00006  * not use this file except in compliance with the License.
00007  * You may obtain a copy of the License at
00008  *
00009  * http://www.apache.org/licenses/LICENSE-2.0
00010  *
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
00013  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  */
00017 #include "uvisor-lib/uvisor-lib.h"
00018 
00019 #if !(defined(UVISOR_PRESENT) && (UVISOR_PRESENT == 1))
00020 
00021 #include "cmsis.h"
00022 
00023 /* This is the fallback implementation for using the page allocator from uVisor
00024  * inside an OS as a normal function.
00025  * Be aware that the page allocator is not re-entrant, so the OS must provide a
00026  * mutex implementation to enable thread-safety!
00027  */
00028 #define DPRINTF(...) {}
00029 #define g_active_box 0
00030 #define vmpu_is_box_id_valid(...) 0
00031 #define vmpu_public_flash_addr(...) 1
00032 #define vmpu_sram_addr(...) 1
00033 #define HALT_ERROR(id, ...) {}
00034 #define UVISOR_PAGE_ALLOCATOR_MUTEX_AQUIRE  page_allocator_mutex_aquire()
00035 #define UVISOR_PAGE_ALLOCATOR_MUTEX_RELEASE osMutexRelease(g_page_allocator_mutex_id)
00036 #define page_allocator_reset_faults(...) {}
00037 
00038 /* Forward declaration of the page allocator API. */
00039 int page_allocator_malloc(UvisorPageTable * const table);
00040 int page_allocator_free(const UvisorPageTable * const table);
00041 
00042 int uvisor_page_malloc(UvisorPageTable *const table)
00043 {
00044     return page_allocator_malloc(table);
00045 }
00046 
00047 int uvisor_page_free(const UvisorPageTable *const table)
00048 {
00049     return page_allocator_free(table);
00050 }
00051 
00052 /* Implement mutex for page allocator. */
00053 static osMutexId g_page_allocator_mutex_id = NULL;
00054 static int32_t g_page_allocator_mutex_data[4];
00055 static const osMutexDef_t g_page_allocator_mutex = { g_page_allocator_mutex_data };
00056 
00057 static void page_allocator_mutex_aquire()
00058 {
00059     if (g_page_allocator_mutex_id == NULL) {
00060         /* Create mutex if not already done. */
00061         g_page_allocator_mutex_id = osMutexCreate(&g_page_allocator_mutex);
00062         if (g_page_allocator_mutex_id == NULL) {
00063             /* Mutex failed to be created. */
00064             return;
00065         }
00066     }
00067 
00068     osMutexWait(g_page_allocator_mutex_id, osWaitForever);
00069 }
00070 
00071 /* Alignment of MPU regions is not required anymore, however we still require
00072  * a 32B alignment, to have some page size granularity. */
00073 static inline int vmpu_is_region_size_valid(uint32_t size)
00074 {
00075     return ((size & ~31) == size);
00076 }
00077 static inline uint32_t vmpu_round_up_region(uint32_t addr, uint32_t size)
00078 {
00079     if (!vmpu_is_region_size_valid(size)) {
00080         return 0;
00081     }
00082     const uint32_t mask = size - 1;
00083     /* Adding the mask can overflow. */
00084     const uint32_t rounded_addr = addr + mask;
00085     /* Check for overflow. */
00086     if (rounded_addr < addr) {
00087         /* This means the address was too large to align. */
00088         return 0;
00089     }
00090     /* Mask the rounded address to get the aligned address. */
00091     return (rounded_addr & ~mask);
00092 }
00093 static inline uint32_t page_table_read(uint32_t addr)
00094 {
00095     return *((uint32_t *) addr);
00096 }
00097 static inline void page_table_write(uint32_t addr, uint32_t data)
00098 {
00099     *((uint32_t *) addr) = data;
00100 }
00101 
00102 /* Include the original page allocator source directly. */
00103 #include "../page_allocator.c_inc"
00104 
00105 #endif