minix/drivers/amddev/amddev.c
Cristiano Giuffrida cb176df60f New RS and new signal handling for system processes.
UPDATING INFO:
20100317:
        /usr/src/etc/system.conf updated to ignore default kernel calls: copy
        it (or merge it) to /etc/system.conf.
        The hello driver (/dev/hello) added to the distribution:
        # cd /usr/src/commands/scripts && make clean install
        # cd /dev && MAKEDEV hello

KERNEL CHANGES:
- Generic signal handling support. The kernel no longer assumes PM as a signal
manager for every process. The signal manager of a given process can now be
specified in its privilege slot. When a signal has to be delivered, the kernel
performs the lookup and forwards the signal to the appropriate signal manager.
PM is the default signal manager for user processes, RS is the default signal
manager for system processes. To enable ptrace()ing for system processes, it
is sufficient to change the default signal manager to PM. This will temporarily
disable crash recovery, though.
- sys_exit() is now split into sys_exit() (i.e. exit() for system processes,
which generates a self-termination signal), and sys_clear() (i.e. used by PM
to ask the kernel to clear a process slot when a process exits).
- Added a new kernel call (i.e. sys_update()) to swap two process slots and
implement live update.

PM CHANGES:
- Posix signal handling is no longer allowed for system processes. System
signals are split into two fixed categories: termination and non-termination
signals. When a non-termination signaled is processed, PM transforms the signal
into an IPC message and delivers the message to the system process. When a
termination signal is processed, PM terminates the process.
- PM no longer assumes itself as the signal manager for system processes. It now
makes sure that every system signal goes through the kernel before being
actually processes. The kernel will then dispatch the signal to the appropriate
signal manager which may or may not be PM.

SYSLIB CHANGES:
- Simplified SEF init and LU callbacks.
- Added additional predefined SEF callbacks to debug crash recovery and
live update.
- Fixed a temporary ack in the SEF init protocol. SEF init reply is now
completely synchronous.
- Added SEF signal event type to provide a uniform interface for system
processes to deal with signals. A sef_cb_signal_handler() callback is
available for system processes to handle every received signal. A
sef_cb_signal_manager() callback is used by signal managers to process
system signals on behalf of the kernel.
- Fixed a few bugs with memory mapping and DS.

VM CHANGES:
- Page faults and memory requests coming from the kernel are now implemented
using signals.
- Added a new VM call to swap two process slots and implement live update.
- The call is used by RS at update time and in turn invokes the kernel call
sys_update().

RS CHANGES:
- RS has been reworked with a better functional decomposition.
- Better kernel call masks. com.h now defines the set of very basic kernel calls
every system service is allowed to use. This makes system.conf simpler and
easier to maintain. In addition, this guarantees a higher level of isolation
for system libraries that use one or more kernel calls internally (e.g. printf).
- RS is the default signal manager for system processes. By default, RS
intercepts every signal delivered to every system process. This makes crash
recovery possible before bringing PM and friends in the loop.
- RS now supports fast rollback when something goes wrong while initializing
the new version during a live update.
- Live update is now implemented by keeping the two versions side-by-side and
swapping the process slots when the old version is ready to update.
- Crash recovery is now implemented by keeping the two versions side-by-side
and cleaning up the old version only when the recovery process is complete.

DS CHANGES:
- Fixed a bug when the process doing ds_publish() or ds_delete() is not known
by DS.
- Fixed the completely broken support for strings. String publishing is now
implemented in the system library and simply wraps publishing of memory ranges.
Ideally, we should adopt a similar approach for other data types as well.
- Test suite fixed.

DRIVER CHANGES:
- The hello driver has been added to the Minix distribution to demonstrate basic
live update and crash recovery functionalities.
- Other drivers have been adapted to conform the new SEF interface.
2010-03-17 01:15:29 +00:00

472 lines
10 KiB
C

/*
amddev.c
Driver for the AMD Device Exclusion Vector (DEV)
*/
#define _SYSTEM
#define _MINIX
#include <minix/config.h>
#include <minix/type.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <machine/vm.h>
#include <signal.h>
#include <minix/com.h>
#include <minix/const.h>
#include <minix/ipc.h>
#include <minix/syslib.h>
#include <minix/sysutil.h>
#include <minix/endpoint.h>
#include <machine/pci.h>
/* Offsets from capability pointer */
#define DEV_OP 4 /* Selects control/status register to access */
#define DEV_OP_FUNC_SHIFT 8 /* Function part in OP reg. */
#define DEV_DATA 8 /* Read/write to access reg. selected */
/* Functions */
#define DEVF_BASE_LO 0
#define DEVF_BASE_HI 1
#define DEVF_MAP 2
#define DEVF_CAP 3
#define DEVF_CAP_MAPS_MASK 0x00ff0000
#define DEVF_CAP_MAPS_SHIFT 16
#define DEVF_CAP_DOMS_MASK 0x0000ff00
#define DEVF_CAP_DOMS_SHIFT 8
#define DEVF_CAP_REV_MASK 0x000000ff
#define DEVF_CAP_REV_SHIFT 0
#define DEVF_CR 4
#define DEVF_ERR_STATUS 5
#define DEVF_ERR_ADDR_LO 6
#define DEVF_ERR_ADDR_HI 7
static int dev_devind;
static u8_t dev_capptr;
static u8_t *table;
static int find_dev(int *devindp, u8_t *capaddrp);
static u32_t read_reg(int function, int index);
static void write_reg(int function, int index, u32_t value);
static void init_domain(int index);
static void init_map(unsigned int ix);
static int do_add4pci(message *m);
static void add_range(u32_t busaddr, u32_t size);
static void del_range(u32_t busaddr, u32_t size);
static void report_exceptions(void);
/* SEF functions and variables. */
FORWARD _PROTOTYPE( void sef_local_startup, (void) );
FORWARD _PROTOTYPE( int sef_cb_init_fresh, (int type, sef_init_info_t *info) );
FORWARD _PROTOTYPE( void sef_cb_signal_handler, (int signo) );
int main(void)
{
int r;
message m;
/* SEF local startup. */
sef_local_startup();
for(;;)
{
report_exceptions();
r= sef_receive(ANY, &m);
if (r != OK)
panic("sef_receive failed: %d", r);
if (m.m_type == IOMMU_MAP) {
r= do_add4pci(&m);
m.m_type= r;
send(m.m_source, &m);
continue;
}
printf("amddev: got message from %d\n", m.m_source);
}
}
/*===========================================================================*
* sef_local_startup *
*===========================================================================*/
PRIVATE void sef_local_startup()
{
/* Register init callbacks. */
sef_setcb_init_fresh(sef_cb_init_fresh);
sef_setcb_init_lu(sef_cb_init_fresh);
sef_setcb_init_restart(sef_cb_init_fresh);
/* Register live update callbacks. */
sef_setcb_lu_prepare(sef_cb_lu_prepare_always_ready);
sef_setcb_lu_state_isvalid(sef_cb_lu_state_isvalid_standard);
/* Register signal callbacks. */
sef_setcb_signal_handler(sef_cb_signal_handler);
/* Let SEF perform startup. */
sef_startup();
}
/*===========================================================================*
* sef_cb_init_fresh *
*===========================================================================*/
PRIVATE int sef_cb_init_fresh(int type, sef_init_info_t *info)
{
/* Initialize the amddev driver. */
int r, n_maps, n_domains, revision;
u16_t flags;
u32_t bits;
printf("amddev: starting\n");
r= find_dev(&dev_devind, &dev_capptr);
if (!r)
return r;
flags= pci_attr_r16(dev_devind, dev_capptr+CAP_SD_INFO);
printf("amddev`init: flags = 0x%x\n", flags);
bits= read_reg(DEVF_CAP, 0);
n_maps= ((bits & DEVF_CAP_MAPS_MASK) >> DEVF_CAP_MAPS_SHIFT);
n_domains= ((bits & DEVF_CAP_DOMS_MASK) >> DEVF_CAP_DOMS_SHIFT);
revision= ((bits & DEVF_CAP_REV_MASK) >> DEVF_CAP_REV_SHIFT);
printf("amddev`init: DEVF_CAP = 0x%x (%d maps, %d domains, rev 0x%x)\n",
bits, n_maps, n_domains, revision);
printf("status = 0x%x, addr-lo = 0x%x, addr-hi = 0x%x\n",
read_reg(DEVF_ERR_STATUS, 0),
read_reg(DEVF_ERR_ADDR_LO, 0),
read_reg(DEVF_ERR_ADDR_HI, 0));
init_domain(0);
init_map(0);
#if 0
init_domain(1);
#endif
write_reg(DEVF_CR, 0, 0x10 | 0x8 | 0x4 | 1);
printf("after write: DEVF_CR: 0x%x\n", read_reg(DEVF_CR, 0));
return(OK);
}
/*===========================================================================*
* sef_cb_signal_handler *
*===========================================================================*/
PRIVATE void sef_cb_signal_handler(int signo)
{
int r;
endpoint_t proc_e;
phys_bytes base, size;
/* Only check for termination signal, ignore anything else. */
if (signo != SIGTERM) return;
for (;;)
{
r= getdma(&proc_e, &base, &size);
if (r == -1)
{
if (errno != -EAGAIN)
{
printf(
"amddev: getdma failed: %d\n",
errno);
}
break;
}
printf(
"amddev: deleting 0x%x@0x%x for proc %d\n",
size, base, proc_e);
del_range(base, size);
r= deldma(proc_e, base, size);
if (r == -1)
{
printf("amddev: deldma failed: %d\n",
errno);
break;
}
}
}
/* Returns 0 if no device found, or 1 if a device is found. */
static int find_dev(devindp, capaddrp)
int *devindp;
u8_t *capaddrp;
{
int r, devind, first;
u8_t capptr, type, next, subtype;
u16_t vid, did, status;
pci_init();
first= 1;
for(;;)
{
if (first)
{
first= 0;
r= pci_first_dev(&devind, &vid, &did);
if (!r)
{
printf("amddev`find_dev: no first dev\n");
return 0;
}
}
else
{
r= pci_next_dev(&devind, &vid, &did);
if (!r)
{
printf("amddev`find_dev: no next dev\n");
return 0;
}
}
printf("amddev`find_dev: got devind %d, vid 0x%x, did 0x%x\n",
devind, vid, did);
/* Check capabilities bit in the device status register */
status= pci_attr_r16(devind, PCI_SR);
if (!(status & PSR_CAPPTR))
continue;
capptr= (pci_attr_r8(devind, PCI_CAPPTR) & PCI_CP_MASK);
while (capptr != 0)
{
type = pci_attr_r8(devind, capptr+CAP_TYPE);
next= (pci_attr_r8(devind, capptr+CAP_NEXT) &
PCI_CP_MASK);
if (type == CAP_T_SECURE_DEV)
{
printf(
"amddev`find_dev: found secure device\n");
subtype= (pci_attr_r8(devind, capptr+
CAP_SD_INFO) & CAP_SD_SUBTYPE_MASK);
if (subtype == CAP_T_SD_DEV)
{
printf("amddev`find_dev: AMD DEV\n");
pci_reserve(devind);
*devindp= devind;
*capaddrp= capptr;
return 1;
}
}
capptr= next;
}
}
return 0;
}
static u32_t read_reg(int function, int index)
{
pci_attr_w32(dev_devind, dev_capptr + DEV_OP, ((function <<
DEV_OP_FUNC_SHIFT) | index));
return pci_attr_r32(dev_devind, dev_capptr + DEV_DATA);
}
static void write_reg(int function, int index, u32_t value)
{
pci_attr_w32(dev_devind, dev_capptr + DEV_OP, ((function <<
DEV_OP_FUNC_SHIFT) | index));
pci_attr_w32(dev_devind, dev_capptr + DEV_DATA, value);
}
static void init_domain(int index)
{
size_t size, memsize;
phys_bytes busaddr;
size= 0x100000 / 8;
table= alloc_contig(size, AC_ALIGN4K, &busaddr);
if (table == NULL)
panic("malloc failed");
if (index == 0)
{
memset(table, 0, size);
memsize= 0x37000 / 8;
printf("memsize = 0x%x / 8\n", memsize*8);
memset(table, 0xff, memsize);
}
else
{
memset(table, 0xff, size);
memset(table, 0x00, size);
}
printf("init_domain: busaddr = %p\n", busaddr);
write_reg(DEVF_BASE_HI, index, 0);
write_reg(DEVF_BASE_LO, index, busaddr | 3);
printf("after write: DEVF_BASE_LO: 0x%x\n",
read_reg(DEVF_BASE_LO, index));
}
static void init_map(unsigned int ix)
{
u32_t v, dom, busno, unit0, unit1;
dom= 1;
busno= 7;
unit1= 9;
unit0= 9;
v= (dom << 26) | (dom << 20) | (busno << 12) |
(0 << 11) | (unit1 << 6) |
(0 << 5) | (unit0 << 0);
write_reg(DEVF_MAP, ix, v);
printf("after write: DEVF_MAP: 0x%x\n", read_reg(DEVF_MAP, ix));
}
#if 0
static int do_add(message *m)
{
int r;
endpoint_t proc;
vir_bytes start;
size_t size;
phys_bytes busaddr;
proc= m->m_source;
start= m->m2_l1;
size= m->m2_l2;
#if 0
printf("amddev`do_add: got request for 0x%x@0x%x from %d\n",
size, start, proc);
#endif
if (start % I386_PAGE_SIZE)
{
printf("amddev`do_add: bad start 0x%x from proc %d\n",
start, proc);
return EINVAL;
}
if (size % I386_PAGE_SIZE)
{
printf("amddev`do_add: bad size 0x%x from proc %d\n",
size, proc);
return EINVAL;
}
r= sys_umap(proc, VM_D, (vir_bytes)start, size, &busaddr);
if (r != OK)
{
printf("amddev`do_add: umap failed for 0x%x@0x%x, proc %d\n",
size, start, proc);
return r;
}
add_range(busaddr, size);
}
#endif
static int do_add4pci(message *m)
{
int r, pci_bus, pci_dev, pci_func;
endpoint_t proc;
vir_bytes start;
size_t size;
phys_bytes busaddr;
proc= m->m_source;
start= m->m2_l1;
size= m->m2_l2;
pci_bus= m->m1_i1;
pci_dev= m->m1_i2;
pci_func= m->m1_i3;
printf(
"amddev`do_add4pci: got request for 0x%x@0x%x from %d for pci dev %u.%u.%u\n",
size, start, proc, pci_bus, pci_dev, pci_func);
if (start % I386_PAGE_SIZE)
{
printf("amddev`do_add4pci: bad start 0x%x from proc %d\n",
start, proc);
return EINVAL;
}
if (size % I386_PAGE_SIZE)
{
printf("amddev`do_add4pci: bad size 0x%x from proc %d\n",
size, proc);
return EINVAL;
}
printf("amddev`do_add4pci: should check with PCI\n");
r= sys_umap(proc, VM_D, (vir_bytes)start, size, &busaddr);
if (r != OK)
{
printf(
"amddev`do_add4pci: umap failed for 0x%x@0x%x, proc %d: %d\n",
size, start, proc, r);
return r;
}
r= adddma(proc, start, size);
if (r != 0)
{
r= -errno;
printf(
"amddev`do_add4pci: adddma failed for 0x%x@0x%x, proc %d: %d\n",
size, start, proc, r);
return r;
}
add_range(busaddr, size);
return OK;
}
static void add_range(u32_t busaddr, u32_t size)
{
u32_t o;
#if 0
printf("add_range: mapping 0x%x@0x%x\n", size, busaddr);
#endif
for (o= 0; o<size; o += I386_PAGE_SIZE)
{
u32_t bit= (busaddr+o)/I386_PAGE_SIZE;
table[bit/8] &= ~(1U << (bit % 8));
}
}
static void del_range(u32_t busaddr, u32_t size)
{
u32_t o, bit;
#if 0
printf("del_range: mapping 0x%x@0x%x\n", size, busaddr);
#endif
for (o= 0; o<size; o += I386_PAGE_SIZE)
{
bit= (busaddr+o)/I386_PAGE_SIZE;
table[bit/8] |= (1 << (bit % 8));
}
}
static void report_exceptions(void)
{
u32_t status;
status= read_reg(DEVF_ERR_STATUS, 0);
if (!(status & 0x80000000))
return;
printf("amddev: status = 0x%x, addr-lo = 0x%x, addr-hi = 0x%x\n",
status, read_reg(DEVF_ERR_ADDR_LO, 0),
read_reg(DEVF_ERR_ADDR_HI, 0));
write_reg(DEVF_ERR_STATUS, 0, 0);
}