////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
//
|
//
|
// Filename: ktraps.h
|
// Filename: ktraps.h
|
//
|
//
|
// Project: CMod S6 System on a Chip, ZipCPU demonstration project
|
// Project: CMod S6 System on a Chip, ZipCPU demonstration project
|
//
|
//
|
// Purpose:
|
// Purpose:
|
//
|
//
|
// Creator: Dan Gisselquist, Ph.D.
|
// Creator: Dan Gisselquist, Ph.D.
|
// Gisselquist Technology, LLC
|
// Gisselquist Technology, LLC
|
//
|
//
|
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
//
|
//
|
// Copyright (C) 2015-2016, Gisselquist Technology, LLC
|
// Copyright (C) 2015-2016, Gisselquist Technology, LLC
|
//
|
//
|
// This program is free software (firmware): you can redistribute it and/or
|
// This program is free software (firmware): you can redistribute it and/or
|
// modify it under the terms of the GNU General Public License as published
|
// modify it under the terms of the GNU General Public License as published
|
// by the Free Software Foundation, either version 3 of the License, or (at
|
// by the Free Software Foundation, either version 3 of the License, or (at
|
// your option) any later version.
|
// your option) any later version.
|
//
|
//
|
// This program is distributed in the hope that it will be useful, but WITHOUT
|
// This program is distributed in the hope that it will be useful, but WITHOUT
|
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or
|
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or
|
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
// for more details.
|
// for more details.
|
//
|
//
|
// You should have received a copy of the GNU General Public License along
|
// You should have received a copy of the GNU General Public License along
|
// with this program. (It's in the $(ROOT)/doc directory, run make with no
|
// with this program. (It's in the $(ROOT)/doc directory, run make with no
|
// target there if the PDF file isn't present.) If not, see
|
// target there if the PDF file isn't present.) If not, see
|
// <http://www.gnu.org/licenses/> for a copy.
|
// <http://www.gnu.org/licenses/> for a copy.
|
//
|
//
|
// License: GPL, v3, as defined and found on www.gnu.org,
|
// License: GPL, v3, as defined and found on www.gnu.org,
|
// http://www.gnu.org/licenses/gpl.html
|
// http://www.gnu.org/licenses/gpl.html
|
//
|
//
|
//
|
//
|
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
//
|
//
|
//
|
//
|
#ifndef KTRAPS_H
|
#ifndef KTRAPS_H
|
#define KTRAPS_H
|
#define KTRAPS_H
|
|
|
typedef enum {
|
typedef enum {
|
// First two deal with interrupts:
|
// First two deal with interrupts:
|
// syscall(TRAPID_WAIT, intmask, timeout, ?)
|
// syscall(TRAPID_WAIT, intmask, timeout, ?)
|
// returns when an interrupt in intmask has taken place
|
// returns when an interrupt in intmask has taken place
|
// may return immediately if such an interrupt has
|
// may return immediately if such an interrupt has
|
// occurred between the last such syscall and this one
|
// occurred between the last such syscall and this one
|
// If timeout is != 0, specifies the maximum amount of
|
// If timeout is != 0, specifies the maximum amount of
|
// time to wait for the event to occurr.
|
// time to wait for the event to occurr.
|
// If intmask = 0, then the task will wait for a timeout
|
// If intmask = 0, then the task will wait for a timeout
|
// alone.
|
// alone.
|
// If the task is stalled for another reason, it may wake
|
// If the task is stalled for another reason, it may wake
|
// early from the trap when the timeout is up.
|
// early from the trap when the timeout is up.
|
// syscall(TRAPID_CLEAR, intmask, timeout, ?)
|
// syscall(TRAPID_CLEAR, intmask, timeout, ?)
|
// Same thing, except this returns immediately after
|
// Same thing, except this returns immediately after
|
// clearing any potentially pending interrupts
|
// clearing any potentially pending interrupts
|
// If the timeout < 0, clears any pending timeout wakeup
|
// If the timeout < 0, clears any pending timeout wakeup
|
// If the timeout > 0, sets a pending timeout wakeup and
|
// If the timeout > 0, sets a pending timeout wakeup and
|
// returns.
|
// returns.
|
// If the timeout == 0, does nothing.
|
// If the timeout == 0, does nothing.
|
TRAPID_WAIT, TRAPID_CLEAR, TRAPID_POST,
|
TRAPID_WAIT, TRAPID_CLEAR, TRAPID_POST,
|
// Yield: Yields the processor until the next scheduled time slice.
|
// Yield: Yields the processor until the next scheduled time slice.
|
TRAPID_YIELD,
|
TRAPID_YIELD,
|
TRAPID_READ, TRAPID_WRITE,
|
TRAPID_READ, TRAPID_WRITE,
|
TRAPID_TIME,
|
TRAPID_TIME,
|
// Return from a kernel system call. This is necessary if ever an
|
// Return from a kernel system call. This is necessary if ever an
|
// exception triggers a syste call. In such cases, it will be
|
// exception triggers a syste call. In such cases, it will be
|
// impossible to return the caller back to his context in a pristine
|
// impossible to return the caller back to his context in a pristine
|
// manner ... without help.
|
// manner ... without help.
|
// TRAPID_KRETURN
|
// TRAPID_KRETURN
|
// Semaphore ID's. These allow us to run the rest of the trap
|
// Semaphore ID's. These allow us to run the rest of the trap
|
// stuffs in kernel space
|
// stuffs in kernel space
|
TRAPID_SEMGET, TRAPID_SEMPUT, TRAPID_SEMNEW,
|
TRAPID_SEMGET, TRAPID_SEMPUT, TRAPID_SEMNEW,
|
// Malloc--since we're using a system level malloc, from a system
|
// Malloc--since we're using a system level malloc, from a system
|
// heap for everything, malloc/free require system calls
|
// heap for everything, malloc/free require system calls
|
TRAPID_MALLOC, TRAPID_FREE,
|
TRAPID_MALLOC, TRAPID_FREE,
|
// EXIT -- end a task
|
// EXIT -- end a task
|
TRAPID_EXIT
|
TRAPID_EXIT
|
} TRAPID;
|
} TRAPID;
|
|
|
extern int syscall(const int id, const int a, const int b, const int c);
|
extern int syscall(const int id, const int a, const int b, const int c);
|
|
|
static inline int read(int fid, void *buf, int ln) {
|
static inline int read(int fid, void *buf, int ln) {
|
return syscall(TRAPID_READ, fid,(int)buf,ln);
|
return syscall(TRAPID_READ, fid,(int)buf,ln);
|
} static inline int write(int fid, const void *buf, int ln) {
|
} static inline int write(int fid, const void *buf, int ln) {
|
return syscall(TRAPID_WRITE, fid, (int)buf, ln);
|
return syscall(TRAPID_WRITE, fid, (int)buf, ln);
|
}
|
}
|
|
|
static inline unsigned time(void) {
|
static inline unsigned time(void) {
|
return syscall(TRAPID_TIME, 0,0,0);
|
return syscall(TRAPID_TIME, 0,0,0);
|
}
|
}
|
|
|
static inline void yield(void) {
|
static inline void yield(void) {
|
syscall(TRAPID_YIELD, 0, 0, 0);
|
syscall(TRAPID_YIELD, 0, 0, 0);
|
} static inline int wait(unsigned event_mask, int timeout) {
|
} static inline int wait(unsigned event_mask, int timeout) {
|
return syscall(TRAPID_WAIT, (int)event_mask, timeout, 0);
|
return syscall(TRAPID_WAIT, (int)event_mask, timeout, 0);
|
} static inline void clear(unsigned event) {
|
} static inline int clear(unsigned event, int timeout) {
|
syscall(TRAPID_CLEAR, (int)event, 0, 0);
|
return syscall(TRAPID_CLEAR, (int)event, timeout, 0);
|
} static inline void post(unsigned event) {
|
} static inline void post(unsigned event) {
|
syscall(TRAPID_POST, (int)event, 0, 0);
|
syscall(TRAPID_POST, (int)event, 0, 0);
|
}
|
}
|
|
|
static inline void *malloc(unsigned nbytes) {
|
static inline void *malloc(unsigned nbytes) {
|
return (void *)syscall(TRAPID_MALLOC, (int)nbytes, 0, 0);
|
return (void *)syscall(TRAPID_MALLOC, (int)nbytes, 0, 0);
|
} static inline void free(void *buf) {
|
} static inline void free(void *buf) {
|
syscall(TRAPID_FREE,(int)buf,0,0);
|
syscall(TRAPID_FREE,(int)buf,0,0);
|
}
|
}
|
|
|
static inline void exit(int status) {
|
static inline void exit(int status) {
|
syscall(TRAPID_EXIT,status,0,0);
|
syscall(TRAPID_EXIT,status,0,0);
|
}
|
}
|
|
|
#endif
|
#endif
|
|
|