Skip to content

Commit

Permalink
core_mmu: add shared memory subsystem
Browse files Browse the repository at this point in the history
This patch adds new shared memory subsystem. It consists of:
 - VA space for shared buffer mappings
 - tee_mm_pool as allocator for that VA space
 - Functions to map/unmap shared buffers

Signed-off-by: Volodymyr Babchuk <vlad.babchuk@gmail.com>
  • Loading branch information
lorc committed Jan 24, 2017
1 parent a6a27e0 commit 732d41c
Show file tree
Hide file tree
Showing 7 changed files with 648 additions and 2 deletions.
1 change: 1 addition & 0 deletions core/arch/arm/include/mm/core_mmu.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ enum teecore_memtypes {
MEM_AREA_IO_NSEC,
MEM_AREA_IO_SEC,
MEM_AREA_RES_VASPACE,
MEM_AREA_SHM_VASPACE,
MEM_AREA_TA_VASPACE,
MEM_AREA_MAXTYPE
};
Expand Down
60 changes: 60 additions & 0 deletions core/arch/arm/include/mm/shmem.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Copyright (c) 2016, EPAM Systems
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef SHMEM_H
#define SHMEM_H

/*
* Shared memory mapping routines. Check shmem.c for documentation.
*/

#include <mm/tee_mmu_types.h>
#include <types_ext.h>

struct shmem_mapping;

int shmem_map_buffer(paddr_t pa, size_t size, paddr_t *pages,
int pages_cnt, struct shmem_mapping **shmem_mapping);

int shmem_map_page(paddr_t page, struct shmem_mapping **shmem_mapping);

void shmem_unmap_buffer(struct shmem_mapping *mapping);
struct shmem_mapping *shmem_find_by_pa(paddr_t paddr);

void shmem_set_cookie(struct shmem_mapping *mapping, uint64_t cookie);
struct shmem_mapping *shmem_get_by_cookie(uint64_t cookie);

bool shmem_contains_region_va(void *va, size_t size);
bool shmem_contains_region_pa(paddr_t pa, size_t size);
bool shmem_intersects_pa(paddr_t pa, size_t size);

void *shmem_pa2va(paddr_t pa);
paddr_t shmem_va2pa(void *va);

paddr_t shmem_get_pa(struct shmem_mapping *mapping);
void *shmem_get_va(struct shmem_mapping *mapping);

#endif
27 changes: 25 additions & 2 deletions core/arch/arm/mm/core_mmu.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
#include <mm/pgt_cache.h>
#include <mm/tee_mmu.h>
#include <mm/tee_pager.h>
#include <mm/shmem.h>
#include <platform_config.h>
#include <stdlib.h>
#include <trace.h>
Expand Down Expand Up @@ -317,6 +318,7 @@ uint32_t core_mmu_type_to_attr(enum teecore_memtypes t)
case MEM_AREA_TA_RAM:
return attr | TEE_MATTR_SECURE | cached;
case MEM_AREA_NSEC_SHM:
case MEM_AREA_SHM_VASPACE:
return attr | cached;
case MEM_AREA_IO_NSEC:
return attr | noncache;
Expand Down Expand Up @@ -355,6 +357,8 @@ static void init_mem_map(struct tee_mmap_region *memory_map, size_t num_elems)

add_va_space(memory_map, num_elems, MEM_AREA_RES_VASPACE,
RES_VASPACE_SIZE, &last);
add_va_space(memory_map, num_elems, MEM_AREA_SHM_VASPACE,
RES_VASPACE_SIZE, &last);

memory_map[last].type = MEM_AREA_NOTYPE;

Expand Down Expand Up @@ -476,6 +480,7 @@ void core_init_mmu_map(void)
case MEM_AREA_RAM_SEC:
case MEM_AREA_RAM_NSEC:
case MEM_AREA_RES_VASPACE:
case MEM_AREA_SHM_VASPACE:
break;
default:
EMSG("Uhandled memtype %d", map->type);
Expand Down Expand Up @@ -534,13 +539,15 @@ bool core_pbuf_is(uint32_t attr, paddr_t pbuf, size_t len)
case CORE_MEM_SEC:
return pbuf_is_inside(secure_only, pbuf, len);
case CORE_MEM_NON_SEC:
return pbuf_is_inside(nsec_shared, pbuf, len);
return pbuf_is_inside(nsec_shared, pbuf, len) ||
shmem_contains_region_pa(pbuf, len);
case CORE_MEM_TEE_RAM:
return pbuf_inside_map_area(pbuf, len, map_tee_ram);
case CORE_MEM_TA_RAM:
return pbuf_inside_map_area(pbuf, len, map_ta_ram);
case CORE_MEM_NSEC_SHM:
return pbuf_inside_map_area(pbuf, len, map_nsec_shm);
return pbuf_inside_map_area(pbuf, len, map_nsec_shm) ||
shmem_contains_region_pa(pbuf, len);
case CORE_MEM_EXTRAM:
return pbuf_is_inside(ddr, pbuf, len);
case CORE_MEM_CACHED:
Expand Down Expand Up @@ -580,13 +587,23 @@ int core_va2pa_helper(void *va, paddr_t *pa)
if (!va_is_in_map(map, (vaddr_t)va))
return -1;

if (map->type == MEM_AREA_SHM_VASPACE) {
*pa = shmem_va2pa(va);
if (*pa)
return 0;
else
return -1;
}

*pa = ((uintptr_t)va & (map->region_size - 1)) |
((map->pa + (uintptr_t)va - map->va) & ~(map->region_size - 1));
return 0;
}

static void *map_pa2va(struct tee_mmap_region *map, paddr_t pa)
{
if (map && map->type == MEM_AREA_SHM_VASPACE)
return shmem_pa2va(pa);
if (!pa_is_in_map(map, pa))
return NULL;
return (void *)((pa & (map->region_size - 1)) |
Expand Down Expand Up @@ -1223,6 +1240,12 @@ void *phys_to_virt(paddr_t pa, enum teecore_memtypes m)
case MEM_AREA_TEE_RAM:
va = phys_to_virt_tee_ram(pa);
break;
case MEM_AREA_NSEC_SHM:
case MEM_AREA_SHM_VASPACE:
va = map_pa2va(find_map_by_type_and_pa(m, pa), pa);
if (!va)
va = shmem_pa2va(pa);
break;
default:
va = map_pa2va(find_map_by_type_and_pa(m, pa), pa);
}
Expand Down
Loading

0 comments on commit 732d41c

Please sign in to comment.