Skip to content

Commit

Permalink
vaguely improve fatal error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
misson20000 committed Dec 14, 2019
1 parent a3360d0 commit 23f601b
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 19 deletions.
10 changes: 8 additions & 2 deletions twili/Threading.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

#include "Threading.hpp"

#include "twili.hpp"

namespace twili {
namespace thread {

Expand Down Expand Up @@ -101,8 +103,12 @@ void EventThread::ThreadEntryShim(void *arg) {
}

void EventThread::ThreadFunc() {
while(!exit_requested) {
ResultCode::AssertOk(event_waiter.Wait(3000000000));
try {
while(!exit_requested) {
ResultCode::AssertOk(event_waiter.Wait(3000000000));
}
} catch(ResultError &e) {
twili::Abort(e);
}
}

Expand Down
4 changes: 2 additions & 2 deletions twili/process/ShellTracker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ ShellTracker::ShellTracker(Twili &twili) :
}
}
if(r.error().code != 0x610 && r.error().code != 0x4e4) { // no events left
ResultCode::AssertOk(std::move(r));
twili::Abort(r.error());
}
return true;
});
Expand Down Expand Up @@ -156,7 +156,7 @@ std::shared_ptr<ShellProcess> ShellTracker::AttachHostProcess(trn::KProcess &&pr

if(created.size() == 0) {
printf(" AHP: no processes created\n");
throw trn::ResultError(TWILI_ERR_APPLET_TRACKER_NO_PROCESS);
twili::Abort(TWILI_ERR_APPLET_TRACKER_NO_PROCESS);
}
proc = created.front();
proc->Attach(std::make_shared<trn::KProcess>(std::move(process)));
Expand Down
75 changes: 60 additions & 15 deletions twili/twili.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,20 +58,17 @@ int main() {
twili::Twili::Config config;

if(config.enable_usb_log) {
if(usb_serial_init() == RESULT_OK) {
// set up serial console
int usb_fd = usb_serial_open_fd();
if(usb_fd < 0) {
throw trn::ResultError(-usb_fd);
}
dup2(usb_fd, STDOUT_FILENO);
dup2(usb_fd, STDERR_FILENO);
dup2(usb_fd, STDIN_FILENO);
dbg_set_file(fd_file_get(usb_fd));
printf("brought up USB serial\n");
} else {
// ignore
ResultCode::AssertOk(usb_serial_init());
// set up serial console
int usb_fd = usb_serial_open_fd();
if(usb_fd < 0) {
throw trn::ResultError(-usb_fd);
}
dup2(usb_fd, STDOUT_FILENO);
dup2(usb_fd, STDERR_FILENO);
dup2(usb_fd, STDIN_FILENO);
dbg_set_file(fd_file_get(usb_fd));
printf("brought up USB serial\n");
}

switch(config.state) {
Expand Down Expand Up @@ -105,15 +102,63 @@ int main() {
printf("done\n");
} catch(trn::ResultError &e) {
std::cout << "caught ResultError: " << e.what() << std::endl;
fatal_init();
fatal_transition_to_fatal_error(e.code.code, 0);
twili::Abort(e);
}

return 0;
}

namespace twili {

void Abort(trn::ResultError &e) {
trn::service::SM sm = ResultCode::AssertOk(trn::service::SM::Initialize());

ipc::client::Object fatal = ResultCode::AssertOk(
sm.GetService("fatal:u"));

uint64_t policy = 0; // ErrorReportAndErrorScreen
if(env_get_kernel_version() >= KERNEL_VERSION_300) {
policy = 2; // ErrorScreen
}

struct FatalContext {
uint64_t x[31] = {0};
uint64_t sp = 0;
uint64_t pc = 0;
uint64_t pstate = 0;
uint64_t afsr0 = 0;
uint64_t afsr1 = 0;
uint64_t esr = 0;
uint64_t far = 0;

uint64_t stack_trace[32] = {0};
uint64_t start_address = 0;
uint64_t register_set_flags = 0;
uint32_t stack_trace_size = 0;

bool is_aarch32 = false;
uint32_t type = 0;
} ctx;

ctx.start_address = (uint64_t) env_get_aslr_base();
ctx.stack_trace_size = std::min<size_t>(32, e.backtrace_size);
std::copy_n(e.backtrace, ctx.stack_trace_size, ctx.stack_trace);

fatal.SendSyncRequest<2>( // ThrowFatalWithCpuContext
ipc::InRaw<uint32_t>(e.code.code),
ipc::InRaw<uint32_t>(policy),
ipc::InRaw<uint64_t>(0),
ipc::InPid(),
ipc::Buffer<FatalContext, 0x15>(&ctx, sizeof(ctx)));

svcExitProcess();
}

void Abort(trn::ResultCode code) {
trn::ResultError e(code);
Abort(e);
}

Twili::Twili(const Config &config) :
config(config),
event_waiter(),
Expand Down
3 changes: 3 additions & 0 deletions twili/twili.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@

namespace twili {

void Abort(trn::ResultError &e);
void Abort(trn::ResultCode code);

class Twili {
public:
struct Config {
Expand Down

0 comments on commit 23f601b

Please sign in to comment.