[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Xen-devel] [PATCH v2] xen: Implement hypercall for tracing of program counters



On Wed, Jul 26, 2017 at 12:43:45PM +0200, Felix Schmoll wrote:
> 
> diff --git a/xen/Kconfig b/xen/Kconfig
> index 65d491d776..5ed2c9c390 100644
> --- a/xen/Kconfig
> +++ b/xen/Kconfig
> @@ -38,4 +38,8 @@ config LTO
>  
>         If unsure, say N.
>  
> +config TRACE_PC
> +    bool "Enable tracing e.g. for fuzzing"
> +    default false

Could use a paragraph to describe what it does. And it probably belongs
to Kconfig.debug.

> +
>  source "Kconfig.debug"
> diff --git a/xen/Rules.mk b/xen/Rules.mk
> index 77bcd44922..dde14e3228 100644
> --- a/xen/Rules.mk
> +++ b/xen/Rules.mk
> @@ -170,6 +170,10 @@ clean:: $(addprefix _clean_, $(subdir-all))
>  _clean_%/: FORCE
>       $(MAKE) -f $(BASEDIR)/Rules.mk -C $* clean
>  
[...]
>  
> diff --git a/xen/arch/x86/pv/hypercall.c b/xen/arch/x86/pv/hypercall.c
> index f79f7eef62..13eb2e86a2 100644
> --- a/xen/arch/x86/pv/hypercall.c
> +++ b/xen/arch/x86/pv/hypercall.c
> @@ -80,6 +80,7 @@ static const hypercall_table_t pv_hypercall_table[] = {
>      HYPERCALL(xenpmu_op),
>      COMPAT_CALL(dm_op),
>      HYPERCALL(mca),
> +    HYPERCALL(trace_pc),
>      HYPERCALL(arch_1),
>  };
>  
> diff --git a/xen/common/Makefile b/xen/common/Makefile
> index 26c5a64337..7f7345cb90 100644
> --- a/xen/common/Makefile
> +++ b/xen/common/Makefile
> @@ -6,6 +6,8 @@ obj-y += cpupool.o
>  obj-$(CONFIG_HAS_DEVICE_TREE) += device_tree.o
>  obj-y += domctl.o
>  obj-y += domain.o
> +obj-y += trace_pc.o
> +obj-$(CONFIG_TRACE_PC) += trace_pc_stub.o

The list of objects is sorted alphabetically. You need to move the new
objects to their right location.

>  obj-y += event_2l.o
>  obj-y += event_channel.o
>  obj-y += event_fifo.o
> @@ -80,3 +82,14 @@ subdir-$(CONFIG_GCOV) += gcov
>  
>  subdir-y += libelf
>  subdir-$(CONFIG_HAS_DEVICE_TREE) += libfdt
[...]
> +#include <xen/xmalloc.h>
> +#include <xen/guest_access.h>
> +#include <xen/sched.h>

Sort these alphabetically.

> +#include <public/trace_pc.h>
> +
> +long do_trace_pc(domid_t dom, int mode, unsigned int size,
> +                 XEN_GUEST_HANDLE_PARAM(uint64_t) buf)
> +{
> +#ifdef CONFIG_TRACE_PC
> +    int ret = 0;
> +    struct domain *d;
> +
> +    if ( dom == DOMID_SELF )
> +        d = current->domain;
> +    else
> +        d = get_domain_by_id(dom);
> +
> +    if ( !d )
> +        return -EINVAL; /* invalid domain */
> +
> +    switch ( mode )
> +    {
> +    case XEN_TRACE_PC_START:
> +    {
> +        if ( d->tracing_buffer )
> +        {
> +            ret = -EBUSY; /* domain already being traced */
> +            break;
> +        }
> +
> +        d->tracing_buffer_pos = 0;
> +        d->tracing_buffer_size = size;
> +        d->tracing_buffer = xmalloc_array(uint64_t, size);
> +
> +        if ( !d->tracing_buffer )
> +            ret = -ENOMEM;
> +        break;
> +    }
> +
> +    case XEN_TRACE_PC_STOP:
> +    {
> +        uint64_t* temp = d->tracing_buffer;

uint64_t *temp;

> +        d->tracing_buffer = NULL;
> +
> +        if ( copy_to_guest(buf, temp, d->tracing_buffer_pos) )
> +            ret = -EFAULT;
> +
> +        xfree(temp);
> +
> +        ret = d->tracing_buffer_pos;
> +        break;
> +    }
> +
> +    default:
> +        ret = -ENOSYS;

EINVAL

> +    }
> +
> +    if ( dom != DOMID_SELF )
> +        put_domain(d);
> +
> +    return ret;
> +#else
> +    return 0;

return -EOPNOTSUPP;

> +#endif
> +}
> +
> +/*
> + * Local variables:
> + * mode: C
> + * c-file-style: "BSD"
> + * c-basic-offset: 4
> + * tab-width: 4
> + * indent-tabs-mode: nil
> + * End:
> + */
> diff --git a/xen/common/trace_pc_stub.c b/xen/common/trace_pc_stub.c
> new file mode 100644
> index 0000000000..4aba7dba9f
> --- /dev/null
> +++ b/xen/common/trace_pc_stub.c
> @@ -0,0 +1,39 @@
> +/******************************************************************************
> + * trace_pc_stub.c
> + *
> + * Edge function/stub for the program counter tracing hypercall.
> + *
> + * Copyright (c) 2017 Felix Schmoll <eggi.innovations@xxxxxxxxx>
> + *
> + * This program is free software; you can redistribute it and/or
> + * modify it under the terms and conditions of the GNU General Public
> + * License, version 2, as published by the Free Software Foundation.
> + *
> + * This program is distributed in the hope that it will be useful,
> + * but WITHOUT ANY WARRANTY; without even the implied warranty of
> + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
> + * General Public License for more details.
> + *
> + * You should have received a copy of the GNU General Public
> + * License along with this program; If not, see 
> <http://www.gnu.org/licenses/>.
> + */
> +
> +#include <xen/trace_pc.h>
> +#include <xen/kernel.h>
> +
> +void __sanitizer_cov_trace_pc(void)
> +{
> +    struct domain *d;
> +
> +    if ( system_state < SYS_STATE_active )
> +        return;
> +
> +    d = current->domain;
> +
> +    if ( d->tracing_buffer &&
> +        (d->tracing_buffer_pos < d->tracing_buffer_size) )
> +    {
> +        d->tracing_buffer[d->tracing_buffer_pos++] =
> +            (uint64_t) __builtin_return_address(0);
> +    }
> +}
> diff --git a/xen/include/public/trace_pc.h b/xen/include/public/trace_pc.h
> new file mode 100644
> index 0000000000..54e430a561
> --- /dev/null
> +++ b/xen/include/public/trace_pc.h
> @@ -0,0 +1,38 @@
> +/******************************************************************************
> + * trace_pc.h
> + *
> + * Macros for program counter tracing hypercall.
> + *
> + * Copyright (C) 2017 Felix Schmoll <eggi.innovations@xxxxxxxxx>
> + *
> + * Permission is hereby granted, free of charge, to any person
> + * obtaining a copy of this software and associated documentation
> + * files (the "Software"), to deal in the Software without restriction,
> + * including without limitation the rights to use, copy, modify, merge,
> + * publish, distribute, sublicense, and/or sell copies of the Software,
> + * and to permit persons to whom the Software is furnished to do so,
> + * subject to the following conditions:
> + *
> + * The above copyright notice and this permission notice shall be
> + * included in all copies or substantial portions of the Software.
> + *
> + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
> + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
> + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
> + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
> + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
> + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
> + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
> + */
> +
> +#ifndef __XEN_PUBLIC_TRACE_PC_H__
> +#define __XEN_PUBLIC_TRACE_PC_H__
> +
> +#if defined(__XEN__) || defined(__XEN_TOOLS__)
> +
> +#define XEN_TRACE_PC_START 0
> +#define XEN_TRACE_PC_STOP 1
> +
> +#endif /* defined(__XEN__) || defined(__XEN_TOOLS__) */
> +
> +#endif /* __XEN_PUBLIC_TRACE_PC_H__ */
> diff --git a/xen/include/public/xen.h b/xen/include/public/xen.h
> index 2ac6b1e24d..95d83c21ce 100644
> --- a/xen/include/public/xen.h
> +++ b/xen/include/public/xen.h
> @@ -121,6 +121,7 @@ DEFINE_XEN_GUEST_HANDLE(xen_ulong_t);
>  #define __HYPERVISOR_xc_reserved_op       39 /* reserved for XenClient */
>  #define __HYPERVISOR_xenpmu_op            40
>  #define __HYPERVISOR_dm_op                41
> +#define __HYPERVISOR_trace_pc             42
>  
>  /* Architecture-specific hypercall definitions. */
>  #define __HYPERVISOR_arch_0               48
> diff --git a/xen/include/xen/hypercall.h b/xen/include/xen/hypercall.h
> index cc99aea57d..aa6269e7b7 100644
> --- a/xen/include/xen/hypercall.h
> +++ b/xen/include/xen/hypercall.h
> @@ -83,6 +83,13 @@ do_xen_version(
>      XEN_GUEST_HANDLE_PARAM(void) arg);
>  
>  extern long
> +do_trace_pc(
> +    domid_t dom_id,
> +    int mode,
> +    unsigned int size,
> +    XEN_GUEST_HANDLE_PARAM(uint64_t) buf);
> +
> +extern long
>  do_console_io(
>      int cmd,
>      int count,
> diff --git a/xen/include/xen/sched.h b/xen/include/xen/sched.h
> index 6673b27d88..4bd3fe2417 100644
> --- a/xen/include/xen/sched.h
> +++ b/xen/include/xen/sched.h
> @@ -483,6 +483,12 @@ struct domain
>          unsigned int guest_request_enabled       : 1;
>          unsigned int guest_request_sync          : 1;
>      } monitor;
> +
> +#ifdef CONFIG_TRACE_PC
> +    uint64_t* tracing_buffer;
> +    unsigned int tracing_buffer_pos;
> +    unsigned int tracing_buffer_size;
> +#endif
>  };
>  
>  /* Protect updates/reads (resp.) of domain_list and domain_hash. */
> diff --git a/xen/include/xen/trace_pc.h b/xen/include/xen/trace_pc.h
> new file mode 100644
> index 0000000000..631815de30
> --- /dev/null
> +++ b/xen/include/xen/trace_pc.h
> @@ -0,0 +1,31 @@
> +/******************************************************************************
> + * trace_pc.h
> + *
> + * Declarations for the program counter tracing hypercall
> + *
> + * Copyright (C) 2017 Felix Schmoll <eggi.innovations@xxxxxxxxx>
> + *
> + * This program is free software; you can redistribute it and/or
> + * modify it under the terms and conditions of the GNU General Public
> + * License, version 2, as published by the Free Software Foundation.
> + *
> + * This program is distributed in the hope that it will be useful,
> + * but WITHOUT ANY WARRANTY; without even the implied warranty of
> + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
> + * General Public License for more details.
> + *
> + * You should have received a copy of the GNU General Public
> + * License along with this program; If not, see 
> <http://www.gnu.org/licenses/>.
> + */
> +
> +#ifndef __TRACE_PC_H__
> +#define __TRACE_PC_H__
> +
> +#include <xen/sched.h>
> +#include <xen/types.h>
> +
> +#include <asm/current.h>
> +
> +void __sanitizer_cov_trace_pc(void);
> +
> +#endif /* __TRACE_PC_H__ */
> -- 
> 2.11.0
> 
> 
> _______________________________________________
> Xen-devel mailing list
> Xen-devel@xxxxxxxxxxxxx
> https://lists.xen.org/xen-devel

_______________________________________________
Xen-devel mailing list
Xen-devel@xxxxxxxxxxxxx
https://lists.xen.org/xen-devel

 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.