2 * (c) 2008-2009 Adam Lackorzynski <adam@os.inf.tu-dresden.de>,
3 * Alexander Warg <warg@os.inf.tu-dresden.de>
4 * economic rights: Technische Universität Dresden (Germany)
6 * This file is part of TUD:OS and distributed under the terms of the
7 * GNU General Public License 2.
8 * Please see the COPYING-GPL-2 file for details.
10 #include "dataspace.h"
11 #include "dataspace_util.h"
13 #include "page_alloc.h"
15 #include <l4/re/dataspace-sys.h>
16 #include <l4/re/protocols>
17 #include <l4/re/util/meta>
19 #include <l4/cxx/ipc_stream>
20 #include <l4/cxx/slab_alloc>
21 #include <l4/cxx/minmax>
23 #include <l4/sys/capability>
24 #include <l4/sys/err.h>
30 Moe::Dataspace::map(l4_addr_t offs, l4_addr_t hot_spot, bool _rw,
31 l4_addr_t min, l4_addr_t max, L4::Snd_fpage &memory)
33 memory = L4::Snd_fpage();
35 offs = l4_trunc_page(offs);
36 hot_spot = l4_trunc_page(hot_spot);
38 if (!check_limit(offs))
41 L4::cout << "MOE: ds access out of bounds: offset=" << L4::n_hex(offs)
42 << " size=" << L4::n_hex(size()) << "\n";
47 Ds_rw rw = _rw ? Writable : Read_only;
48 Address adr = address(offs, rw, hot_spot, min, max);
52 memory = L4::Snd_fpage(adr.fp(), hot_spot, L4::Snd_fpage::Map,
53 (L4::Snd_fpage::Cacheopt)((_flags >> 12) & (7 << 4)));
59 L4::Ipc_ostream &operator << (L4::Ipc_ostream &s,
60 L4Re::Dataspace::Stats const &st)
61 { s.put(st); return s; }
64 Moe::Dataspace::dispatch(l4_umword_t obj, L4::Ipc_iostream &ios)
69 if (tag.label() == L4::Meta::Protocol)
70 return L4Re::Util::handle_meta_request<L4Re::Dataspace>(ios);
72 if (tag.label() != L4Re::Protocol::Dataspace)
78 L4::cout << "MOE: DS: op=" << L4::n_hex(op) << "\n";
83 case L4Re::Dataspace_::Map:
85 // L4_FPAGE_X means writable for DSs!
86 bool read_only = !is_writable() || !(obj & L4_FPAGE_X);
87 l4_addr_t offset, spot;
90 ios >> offset >> spot >> flags;
92 L4::cout << "MAPrq: " << L4::hex << offset << ", " << spot << ", "
96 if (read_only && (flags & Writable))
99 long int ret = map(offset, spot, flags & Writable, 0, ~0, fp);
101 L4::cout << "MAP: " << L4::hex << reinterpret_cast<unsigned long *>(&fp)[0]
102 << ", " << reinterpret_cast<unsigned long *>(&fp)[1]
103 << ", " << flags << ", " << (!read_only && (flags & 1))
104 << ", ret=" << ret << '\n';
111 case L4Re::Dataspace_::Clear:
113 if (!(obj & L4_FPAGE_X) /* read only*/
121 return clear(offs, sz);
123 case L4Re::Dataspace_::Stats:
125 L4Re::Dataspace::Stats s;
127 // only return writable if really writable
128 s.flags = flags() & ~Writable;
129 if ((obj & L4_FPAGE_X) && is_writable())
135 case L4Re::Dataspace_::Copy:
138 Moe::Dataspace *src = 0;
141 L4::Snd_fpage src_cap;
143 ios >> dst_offs >> src_offs >> sz >> src_cap;
145 if (src_cap.id_received())
146 src = dynamic_cast<Moe::Dataspace*>(object_pool.find(src_cap.data()));
148 if (!(obj & L4_FPAGE_X))
157 Dataspace_util::copy(this, dst_offs, src, src_offs, sz);
161 case L4Re::Dataspace_::Phys:
169 int ret = phys(offset, phys_addr, phys_size);
173 ios << phys_addr << phys_size;
177 case L4Re::Dataspace_::Allocate:
181 ios >> offset >> size;
182 return pre_allocate(offset, size, obj & 3);
184 case L4Re::Dataspace_::Take:
186 //L4::cout << "MOE: T[" << this << "]: refs=" << ref_cnt() << '\n';
188 case L4Re::Dataspace_::Release:
189 if (release() == 0 && !is_static())
191 //L4::cout << "MOE: R[" << this << "]: refs=" << ref_cnt() << '\n';
195 //L4::cout << "MOE: R[" << this << "]: refs=" << ref_cnt() << '\n';
204 Moe::Dataspace::clear(l4_addr_t offs, unsigned long _size) const throw()
206 if (!check_limit(offs))
209 unsigned long sz = _size = min(_size, round_size()-offs);
213 Address dst_a = address(offs, Writable);
214 unsigned long b_sz = min(dst_a.sz() - dst_a.of(), sz);
216 memset(dst_a.adr(), 0, b_sz);
226 Moe::Dataspace::phys(l4_addr_t /*offset*/, l4_addr_t &/*phys_addr*/, l4_size_t &/*phys_size*/) throw()