/******************************************************************************
* common/trace.c
*
* Xen Trace Buffer
*
* Copyright (C) 2004 by Intel Research Cambridge
*
* Authors: Mark Williamson, mark.a.williamson@intel.com
* Rob Gardner, rob.gardner@hp.com
* Date: October 2005
*
* Copyright (C) 2005 Bin Ren
*
* The trace buffer code is designed to allow debugging traces of Xen to be
* generated on UP / SMP machines. Each trace entry is timestamped so that
* it's possible to reconstruct a chronological record of trace events.
*
* See also include/xen/trace.h and the dom0 op in
* include/public/dom0_ops.h
*/
#include <xen/config.h>
#include <asm/types.h>
#include <asm/io.h>
#include <xen/lib.h>
#include <xen/sched.h>
#include <xen/smp.h>
#include <xen/trace.h>
#include <xen/errno.h>
#include <xen/init.h>
#include <asm/atomic.h>
#include <public/dom0_ops.h>
/* opt_tbuf_size: trace buffer size (in pages) */
static unsigned int opt_tbuf_size = 0;
integer_param("tbuf_size", opt_tbuf_size);
/* Pointers to the meta-data objects for all system trace buffers */
static struct t_buf *t_bufs[NR_CPUS];
static struct t_rec *t_recs[NR_CPUS];
static int nr_recs;
/* a flag recording whether initialization has been done */
/* or more properly, if the tbuf subsystem is enabled right now */
int tb_init_done;
/* which CPUs tracing is enabled on */
static unsigned long tb_cpu_mask = (~0UL);
/* which tracing events are enabled */
static u32 tb_event_mask = TRC_ALL;
/**
* alloc_trace_bufs - performs initialization of the per-cpu trace buffers.
*
* This function is called at start of day in order to initialize the per-cpu
* trace buffers. The trace buffers are then available for debugging use, via
* the %TRACE_xD macros exported in <xen/trace.h>.
*
* This function may also be called later when enabling trace buffers
* via the SET_SIZE hypercall.
*/
static int alloc_trace_bufs(void)
{
int i, order;
unsigned long nr_pages;
char *rawbuf;
struct t_buf *buf;
if ( opt_tbuf_size == 0 )
return -EINVAL;
nr_pages = num_online_cpus() * opt_tbuf_size;
order = get_order_from_pages(nr_pages);
nr_recs = (opt_tbuf_size * PAGE_SIZE - sizeof(struct t_buf)) /
sizeof(struct t_rec);
if ( (rawbuf = alloc_xenheap_pages(order)) == NULL )
{
printk("Xen trace buffers: memory allocation failed\n");
return -EINVAL;
}
/* Share pages so that xentrace can map them. */
for ( i = 0; i < nr_pages; i++ )
SHARE_PFN_WITH_DOMAIN(virt_to_page(rawbuf + i * PAGE_SIZE), dom0);
for_each_online_cpu ( i )
{
buf = t_bufs[i] = (struct t_buf *)&rawbuf[i*opt_tbuf_size*PAGE_SIZE];
buf->cons = buf->prod = 0;
t_recs[i] = (struct t_rec *)(buf + 1);
}
return 0;
}
/**
* tb_set_size - handle the logic involved with dynamically
* allocating and deallocating tbufs
*
* This function is called when the SET_SIZE hypercall is done.
*/
static int tb_set_size(int size)
{
/*
* Setting size is a one-shot operation. It can be done either at
* boot time or via control tools, but not by both. Once buffers
* are created they cannot be destroyed.
*/
if ( (opt_tbuf_size != 0) || (size <= 0) )
{
DPRINTK("tb_set_size from %d to %d not implemented\n",
opt_tbuf_size, size);
return -EINVAL;
}
opt_tbuf_size = size;
if ( alloc_trace_bufs() != 0 )
{
opt_tbuf_size = 0;
return -EINVAL;
}
printk("Xen trace buffers: initialized\n");
return 0;
}
/**
* init_trace_bufs - performs initialization of the per-cpu trace buffers.
*
* This function is called at start of day in order to initialize the per-cpu
* trace buffers. The trace buffers are then available for debugging use, via
* the %TRACE_xD macros exported in <xen/trace.h>.
*/
void init_trace_bufs(void)
{
if ( opt_tbuf_size == 0 )
{
printk("Xen trace buffers: disabled\n");
return;
}
if ( alloc_trace_bufs() == 0 )
{
printk("Xen trace buffers: initialised\n");
wmb(); /* above must be visible before tb_init_done flag set */
tb_init_done = 1;
}
}
/**
* tb_control - DOM0 operations on trace buffers.
* @tbc: a pointer to a dom0_tbufcontrol_t to be filled out
*/
int tb_control(dom0_tbufcontrol_t *tbc)
{
static spinlock_t lock = SPIN_LOCK_UNLOCKED;
int rc = 0;
spin_lock(&lock);
if ( !tb_init_done &&
(tbc->op != DOM0_TBUF_SET_SI/*
LUFA Library
Copyright (C) Dean Camera, 2017.
dean [at] fourwalledcubicle [dot] com
www.lufa-lib.org
*/
/*
Copyright 2017 Dean Camera (dean [at] fourwalledcubicle [dot] com)
Permission to use, copy, modify, distribute, and sell this
software and its documentation for any purpose is hereby granted
without fee, provided that the above copyright notice appear in
all copies and that both that the copyright notice and this
permission notice and warranty disclaimer appear in supporting
documentation, and that the name of the author not be used in
advertising or publicity pertaining to distribution of the
software without specific, written prior permission.
The author disclaims all warranties with regard to this
software, including all implied warranties of merchantability
and fitness. In no event shall the author be liable for any
special, indirect or consequential damages or any damages
whatsoever resulting from loss of use, data or profits, whether
in an action of contract, negligence or other tortious action,
arising out of or in connection with the use or performance of
this software.
*/
/** \file
*
* Main source file for the DFU class bootloader. This file contains the complete bootloader logic.
*/
#define INCLUDE_FROM_BOOTLOADER_C
#include "BootloaderDFU.h"
/** Flag to indicate if the bootloader is currently running in secure mode, disallowing memory operations
* other than erase. This is initially set to the value set by SECURE_MODE, and cleared by the bootloader
* once a memory erase has completed in a bootloader session.
*/
static bool IsSecure = SECURE_MODE;
/** Flag to indicate if the bootloader should be running, or should exit and allow the application code to run
* via a soft reset. When cleared, the bootloader will abort, the USB interface will shut down and the application
* jumped to via an indirect jump to location 0x0000 (or other location specified by the host).
*/
static bool RunBootloader = true;
/** Flag to indicate if the bootloader is waiting to exit. When the host requests the bootloader to exit and
* jump to the application address it specifies, it sends two sequential commands which must be properly
* acknowledged. Upon reception of the first the RunBootloader flag is cleared and the WaitForExit flag is set,
* causing the bootloader to wait for the final exit command before shutting down.
*/
static bool WaitForExit = false;
/** Current DFU state machine state, one of the values in the DFU_State_t enum. */
static uint8_t DFU_State = dfuIDLE;
/** Status code of the last executed DFU command. This is set to one of the values in the DFU_Status_t enum after
* each operation, and returned to the host when a Get Status DFU request is issued.
*/
static uint8_t DFU_Status = OK;
/** Data containing the DFU command sent from the host. */
static DFU_Command_t SentCommand;
/** Response to the last issued Read Data DFU command. Unlike other DFU commands, the read command
* requires a single byte response from the bootloader containing the read data when the next DFU_UPLOAD command
* is issued by the host.
*/
static uint8_t ResponseByte;
/** Pointer to the start of the user application. By default this is 0x0000 (the reset vector), however the host
* may specify an alternate address when issuing the application soft-start command.
*/
static AppPtr_t AppStartPtr = (AppPtr_t)0x0000;
/** 64-bit flash page number. This is concatenated with the current 16-bit address on USB AVRs containing more than
* 64KB of flash memory.
*/
static uint8_t Flash64KBPage = 0;
/** Memory start address, indicating the current address in the memory being addressed (either FLASH or EEPROM
* depending on the issued command from the host).
*/
static uint16_t StartAddr = 0x0000;
/** Memory end address, indicating the end address to read from/write to in the memory being addressed (either FLASH
* of EEPROM depending on the issued command from the host).
*/
static uint16_t EndAddr = 0x0000;
/** Magic lock for forced application start. If the HWBE fuse is programmed and BOOTRST is unprogrammed, the bootloader
* will start if the /HWB line of the AVR is held low