Kristofer Jonsson | 6d80c42 | 2021-10-14 10:09:08 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2021 Arm Limited. All rights reserved. |
| 3 | * |
| 4 | * SPDX-License-Identifier: Apache-2.0 |
| 5 | * |
| 6 | * Licensed under the Apache License, Version 2.0 (the License); you may |
| 7 | * not use this file except in compliance with the License. |
| 8 | * You may obtain a copy of the License at |
| 9 | * |
| 10 | * www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, software |
| 13 | * distributed under the License is distributed on an AS IS BASIS, WITHOUT |
| 14 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | * See the License for the specific language governing permissions and |
| 16 | * limitations under the License. |
| 17 | */ |
| 18 | |
| 19 | #include "dev_mem.hpp" |
| 20 | |
| 21 | #include <cstddef> |
| 22 | #include <iostream> |
| 23 | |
| 24 | #include <fcntl.h> |
| 25 | #include <poll.h> |
| 26 | #include <sys/ioctl.h> |
| 27 | #include <sys/mman.h> |
| 28 | #include <unistd.h> |
| 29 | |
| 30 | namespace EthosU { |
| 31 | namespace DevMem { |
| 32 | |
| 33 | /**************************************************************************** |
| 34 | * Exception |
| 35 | ****************************************************************************/ |
| 36 | |
| 37 | Exception::Exception(const char *msg) : msg(msg) {} |
| 38 | |
| 39 | Exception::Exception(const std::string &msg) : msg(msg) {} |
| 40 | |
| 41 | Exception::~Exception() throw() {} |
| 42 | |
| 43 | const char *Exception::what() const throw() { |
| 44 | return msg.c_str(); |
| 45 | } |
| 46 | |
| 47 | /**************************************************************************** |
| 48 | * DevMem |
| 49 | ****************************************************************************/ |
| 50 | |
| 51 | DevMem::DevMem(uintptr_t address, size_t size) : |
| 52 | base(nullptr), pageMask(sysconf(_SC_PAGESIZE) - 1), pageOffset(address & pageMask), size(size) { |
| 53 | int fd = ::open("/dev/mem", O_RDWR | O_SYNC); |
| 54 | if (fd < 0) { |
| 55 | throw Exception("Failed to open device"); |
| 56 | } |
| 57 | |
| 58 | base = reinterpret_cast<char *>( |
| 59 | ::mmap(nullptr, pageOffset + size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, address & ~pageMask)); |
| 60 | if (base == MAP_FAILED) { |
| 61 | throw Exception("MMap failed"); |
| 62 | } |
| 63 | |
| 64 | ::close(fd); |
| 65 | } |
| 66 | |
| 67 | DevMem::~DevMem() { |
| 68 | ::munmap(base, pageOffset + size); |
| 69 | } |
| 70 | |
| 71 | void DevMem::read(char *dst, size_t length, size_t offset) { |
| 72 | if (offset + length > size) { |
| 73 | throw Exception("Read failed"); |
| 74 | } |
| 75 | |
| 76 | // TODO Why do not std::copy() or memcpy work? |
| 77 | for (size_t i = 0; i < length; i++) { |
| 78 | dst[i] = base[pageOffset + offset + i]; |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | void DevMem::write(char *src, size_t length, size_t offset) { |
| 83 | if (offset + length > size) { |
| 84 | throw Exception("Write failed"); |
| 85 | } |
| 86 | |
| 87 | // TODO Why do not std::copy() or memcpy work? |
| 88 | for (size_t i = 0; i < length; i++) { |
| 89 | base[pageOffset + offset + i] = src[i]; |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | /**************************************************************************** |
| 94 | * Log |
| 95 | ****************************************************************************/ |
| 96 | |
| 97 | Log::Log(uintptr_t address, size_t size) : DevMem(address, size) {} |
| 98 | |
| 99 | void Log::clear() { |
| 100 | LogHeader header; |
| 101 | read(header, 0); |
| 102 | |
| 103 | uint32_t rpos = header.write; |
| 104 | write(rpos, offsetof(LogHeader, read)); |
| 105 | } |
| 106 | |
| 107 | void Log::print() { |
| 108 | LogHeader header; |
| 109 | read(header, 0); |
| 110 | |
| 111 | if (header.size < LOG_SIZE_MIN || header.size > LOG_SIZE_MAX) { |
| 112 | std::string msg = "Incorrect ring buffer values. size=" + std::to_string(header.size) + |
| 113 | ", read=" + std::to_string(header.read) + ", write=" + std::to_string(header.write); |
| 114 | throw Exception(msg.c_str()); |
| 115 | } |
| 116 | |
| 117 | size_t rpos = header.read; |
| 118 | |
| 119 | // Skip forward if read is more than 'size' behind |
| 120 | if (rpos + header.size < header.write) { |
| 121 | rpos = header.write - header.size; |
| 122 | } |
| 123 | |
| 124 | while (rpos < header.write) { |
| 125 | char c; |
| 126 | size_t offset = rpos++ % header.size + sizeof(header); |
| 127 | read(c, offset); |
| 128 | std::cout << c; |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | } // namespace DevMem |
| 133 | } // namespace EthosU |