2 * romain_pagefault.cc --
4 * Implementation of page fault handling.
6 * (c) 2011 Björn Döbel <doebel@os.inf.tu-dresden.de>,
7 * economic rights: Technische Universität Dresden (Germany)
8 * This file is part of TUD:OS and distributed under the terms of the
9 * GNU General Public License 2.
10 * Please see the COPYING-GPL-2 file for details.
13 #include "../constants.h"
15 #include "../exceptions"
17 #include "../emulation"
18 #include "../app_loading"
19 #include "../locking.h"
20 #include "../configuration"
22 #include "observers.h"
24 #include <l4/sys/kdebug.h>
25 #include <l4/util/bitops.h>
27 extern l4_addr_t __libc_l4_gettime;
29 static int pf_write = 0; // statistical counter
30 static int pf_mapped = 0; // statistical counter
31 static int pf_kip = 0; // statistical counter
32 #define MSG() DEBUGf(Romain::Log::Memory) << "[" << i->id() << "] "
34 /*****************************************************************
35 * Page Fault Handling *
36 *****************************************************************/
37 DEFINE_EMPTY_STARTUP(PageFaultObserver)
39 void Romain::PageFaultObserver::status() const
41 INFO() << "Page faults so far: mapped " << pf_mapped << " write emu "
42 << pf_write << " kip " << pf_kip;
45 Romain::Observer::ObserverReturnVal
46 Romain::PageFaultObserver::notify(Romain::App_instance *i, Romain::App_thread *t, Romain::App_model *a)
48 if (!t->vcpu()->is_page_fault_entry())
49 return Romain::Observer::Ignored;
51 if (t->unhandled_pf()) {
52 enter_kdebug("unhandled pf");
55 L4vcpu::Vcpu *vcpu = t->vcpu();
57 bool write_pf = vcpu->r()->err & 0x2;
58 l4_addr_t pfa = vcpu->r()->pfa;
60 MSG() << (write_pf ? "\033[31mwrite\033[0m" : "\033[34;1mread\033[0m")
61 << " page fault @ 0x" << std::hex << pfa;
63 Romain::Region_map::Base::Node n = a->rm()->find(pfa);
64 MSG() << "rm_find(" << std::hex << pfa << ") = " << n;
66 a->rm()->lazy_map_region(n, i->id());
69 * Lazily establish region handlers for replicas
71 MSG() << "[" << std::hex
72 << n->second.local_region(i->id()).start() << " - "
73 << n->second.local_region(i->id()).end() << "] ==> "
74 << "[" << n->first.start() << " - "
75 << n->first.end() << "]";
76 MSG() << " DS " << std::hex << n->second.memory(i->id()).cap();
78 if (write_pf && (always_readonly() ||
79 (n->second.writable() == Romain::Region_handler::Read_only_emulate_write))) {
81 Romain::WriteEmulator(vcpu, Romain::AppModelAddressTranslator(a, i)).emulate();
82 // writes are emulated, no need to map here at all
83 // -> XXX actually, we could also map() here, because if the client
84 // writes to memory, it's most probably going to read this
85 // data at a later point in time, too
88 * For emulated operations single-stepping won't work, because
89 * the real instruction is never executed in the target AS. Therefore,
90 * we need to inject a "virtual" INT1 into the manager here.
92 if (t->vcpu()->r()->flags & TrapFlag)
93 t->set_pending_trap(1);
97 l4_addr_t offset_in_region = l4_trunc_page(pfa - n->first.start());
98 MSG() << "offs in region: " << std::hex << offset_in_region;
100 // set flags properly, only check ro(), because else we'd already ended
101 // up in the emulation branch above
102 unsigned pageflags = n->second.is_ro() ? L4_FPAGE_RO : L4_FPAGE_RW;
103 unsigned map_size = L4_PAGESIZE;
104 unsigned size_left_in_region = n->first.end() - (n->first.start() + offset_in_region);
107 #define MAX_MAP_SHIFT 0
108 for (unsigned x = 1; x < MAX_MAP_SHIFT; ++x) {
109 if (size_left_in_region >= (1 << (L4_PAGESHIFT + x)))
115 i->map(n->second.local_region(i->id()).start() + offset_in_region, // local addr
116 n->first.start() + offset_in_region, // remote addr
117 pageflags, map_size);
120 } else if ((a->prog_info()->kip <= pfa) && (pfa < a->prog_info()->kip + L4_PAGESIZE)) {
122 MSG() << "KIP access ";
124 MSG() << std::hex << __libc_l4_gettime << " " << *(l4_addr_t*)__libc_l4_gettime;
125 t->set_unhandled_pf();
127 ERROR() << "Unhandled page fault @ address 0x" << std::hex << pfa
128 << " PC @ 0x" << vcpu->r()->ip;
129 t->set_unhandled_pf();
133 * XXX: For now, tell the manager to let all instances perform their fault handling
134 * independently. In fact, we could however also handle all faults upon first
135 * occurrence and return Replicatable here.
137 MSG() << "Page faults so far: mapped " << pf_mapped << " write emu " << pf_write << " kip " << pf_kip;
138 return Romain::Observer::Finished;
142 Romain::PageFaultObserver::PageFaultObserver()
144 char const *ro = ConfigStringValue("general:page_fault_handling");
146 if (ro && (strcmp(ro, "ro") == 0)) {