The Pedigree Project 0.1
MemoryMappedIo.cc
1/*
2 * Copyright (c) 2008-2014, Pedigree Developers
3 *
4 * Please see the CONTRIB file in the root of the source tree for a full
5 * list of contributors.
6 *
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 */
19
20#include "pedigree/kernel/processor/MemoryMappedIo.h"
21#include "pedigree/kernel/processor/Processor.h"
22#include "pedigree/kernel/utilities/utility.h"
23
24MemoryMappedIo::MemoryMappedIo(const char* pName, uintptr_t offset, uintptr_t padding)
25 : IoBase(), MemoryRegion(pName), m_Offset(offset), m_Padding(padding) {}
26
28
29size_t MemoryMappedIo::size() const {
30 return MemoryRegion::size();
31}
32
33uint8_t MemoryMappedIo::read8(size_t offset) {
34#if ADDITIONAL_CHECKS
35 if (offset >= size())
37#endif
38
39 return *reinterpret_cast<volatile uint8_t*>(
40 adjust_pointer(virtualAddress(), (offset * m_Padding) + m_Offset));
41}
42
43uint16_t MemoryMappedIo::read16(size_t offset) {
44#if ADDITIONAL_CHECKS
45 if ((offset + 1) >= size())
47#endif
48
49 return *reinterpret_cast<volatile uint16_t*>(
50 adjust_pointer(virtualAddress(), (offset * m_Padding) + m_Offset));
51}
52
53uint32_t MemoryMappedIo::read32(size_t offset) {
54#if ADDITIONAL_CHECKS
55 if ((offset + 3) >= size())
57#endif
58
59 return *reinterpret_cast<volatile uint32_t*>(
60 adjust_pointer(virtualAddress(), (offset * m_Padding) + m_Offset));
61}
62
63uint64_t MemoryMappedIo::read64(size_t offset) {
64#if ADDITIONAL_CHECKS
65 if ((offset + 7) >= size())
67#endif
68
69 return *reinterpret_cast<volatile uint64_t*>(
70 adjust_pointer(virtualAddress(), (offset * m_Padding) + m_Offset));
71}
72
73void MemoryMappedIo::write8(uint8_t value, size_t offset) {
74#if ADDITIONAL_CHECKS
75 if (offset >= size())
77#endif
78
79 *reinterpret_cast<volatile uint8_t*>(
80 adjust_pointer(virtualAddress(), (offset * m_Padding) + m_Offset)) = value;
81}
82
83void MemoryMappedIo::write16(uint16_t value, size_t offset) {
84#if ADDITIONAL_CHECKS
85 if ((offset + 1) >= size())
87#endif
88
89 *reinterpret_cast<volatile uint16_t*>(
90 adjust_pointer(virtualAddress(), (offset * m_Padding) + m_Offset)) = value;
91}
92
93void MemoryMappedIo::write32(uint32_t value, size_t offset) {
94#if ADDITIONAL_CHECKS
95 if ((offset + 3) >= size())
97#endif
98
99 *reinterpret_cast<volatile uint32_t*>(
100 adjust_pointer(virtualAddress(), (offset * m_Padding) + m_Offset)) = value;
101}
102
103void MemoryMappedIo::write64(uint64_t value, size_t offset) {
104#if ADDITIONAL_CHECKS
105 if ((offset + 7) >= size())
107#endif
108
109 *reinterpret_cast<volatile uint64_t*>(
110 adjust_pointer(virtualAddress(), (offset * m_Padding) + m_Offset)) = value;
111}
112
113MemoryMappedIo::operator bool() const {
114 return MemoryRegion::operator bool();
115}
Abstrace base class for hardware I/O capabilities.
Definition IoBase.h:32
virtual void write8(uint8_t value, size_t offset=0)
virtual uint32_t read32(size_t offset=0)
virtual void write64(uint64_t value, size_t offset=0)
virtual uint16_t read16(size_t offset=0)
virtual uint64_t read64(size_t offset=0)
uintptr_t m_Offset
virtual size_t size() const
virtual uint8_t read8(size_t offset=0)
virtual void write16(uint16_t value, size_t offset=0)
virtual ~MemoryMappedIo()
virtual void write32(uint32_t value, size_t offset=0)
Special memory entity in the kernel's virtual address space.
void * virtualAddress() const
size_t size() const
static void halt()