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 
24 MemoryMappedIo::MemoryMappedIo(
25  const char *pName, uintptr_t offset, uintptr_t padding)
26  : IoBase(), MemoryRegion(pName), m_Offset(offset), m_Padding(padding)
27 {
28 }
29 
31 {
32 }
33 
34 size_t MemoryMappedIo::size() const
35 {
36  return MemoryRegion::size();
37 }
38 
39 uint8_t MemoryMappedIo::read8(size_t offset)
40 {
41 #if defined(ADDITIONAL_CHECKS)
42  if (offset >= size())
44 #endif
45 
46  return *reinterpret_cast<volatile uint8_t *>(
47  adjust_pointer(virtualAddress(), (offset * m_Padding) + m_Offset));
48 }
49 
50 uint16_t MemoryMappedIo::read16(size_t offset)
51 {
52 #if defined(ADDITIONAL_CHECKS)
53  if ((offset + 1) >= size())
55 #endif
56 
57  return *reinterpret_cast<volatile uint16_t *>(
58  adjust_pointer(virtualAddress(), (offset * m_Padding) + m_Offset));
59 }
60 
61 uint32_t MemoryMappedIo::read32(size_t offset)
62 {
63 #if defined(ADDITIONAL_CHECKS)
64  if ((offset + 3) >= size())
66 #endif
67 
68  return *reinterpret_cast<volatile uint32_t *>(
69  adjust_pointer(virtualAddress(), (offset * m_Padding) + m_Offset));
70 }
71 
72 uint64_t MemoryMappedIo::read64(size_t offset)
73 {
74 #if defined(ADDITIONAL_CHECKS)
75  if ((offset + 7) >= size())
77 #endif
78 
79  return *reinterpret_cast<volatile uint64_t *>(
80  adjust_pointer(virtualAddress(), (offset * m_Padding) + m_Offset));
81 }
82 
83 void MemoryMappedIo::write8(uint8_t value, size_t offset)
84 {
85 #if defined(ADDITIONAL_CHECKS)
86  if (offset >= size())
88 #endif
89 
90  *reinterpret_cast<volatile uint8_t *>(adjust_pointer(
91  virtualAddress(), (offset * m_Padding) + m_Offset)) = value;
92 }
93 
94 void MemoryMappedIo::write16(uint16_t value, size_t offset)
95 {
96 #if defined(ADDITIONAL_CHECKS)
97  if ((offset + 1) >= size())
99 #endif
100 
101  *reinterpret_cast<volatile uint16_t *>(adjust_pointer(
102  virtualAddress(), (offset * m_Padding) + m_Offset)) = value;
103 }
104 
105 void MemoryMappedIo::write32(uint32_t value, size_t offset)
106 {
107 #if defined(ADDITIONAL_CHECKS)
108  if ((offset + 3) >= size())
109  Processor::halt();
110 #endif
111 
112  *reinterpret_cast<volatile uint32_t *>(adjust_pointer(
113  virtualAddress(), (offset * m_Padding) + m_Offset)) = value;
114 }
115 
116 void MemoryMappedIo::write64(uint64_t value, size_t offset)
117 {
118 #if defined(ADDITIONAL_CHECKS)
119  if ((offset + 7) >= size())
120  Processor::halt();
121 #endif
122 
123  *reinterpret_cast<volatile uint64_t *>(adjust_pointer(
124  virtualAddress(), (offset * m_Padding) + m_Offset)) = value;
125 }
126 
127 MemoryMappedIo::operator bool() const
128 {
129  return MemoryRegion::operator bool();
130 }
virtual void write64(uint64_t value, size_t offset=0)
virtual uint64_t read64(size_t offset=0)
virtual void write16(uint16_t value, size_t offset=0)
virtual size_t size() const
Abstrace base class for hardware I/O capabilities.
Definition: IoBase.h:31
virtual uint8_t read8(size_t offset=0)
virtual void write32(uint32_t value, size_t offset=0)
Special memory entity in the kernel&#39;s virtual address space.
Definition: MemoryRegion.h:35
size_t size() const
Definition: MemoryRegion.cc:49
virtual uint16_t read16(size_t offset=0)
static void halt()
virtual void write8(uint8_t value, size_t offset=0)
virtual ~MemoryMappedIo()
virtual uint32_t read32(size_t offset=0)