Skip to content

Commit

Permalink
Revert "updates for new build system" (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
brendank310 authored Jun 24, 2016
1 parent c8470c8 commit 10d61d4
Show file tree
Hide file tree
Showing 13 changed files with 415 additions and 95 deletions.
5 changes: 0 additions & 5 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
# Change Log

## [Unreleased]
### Added
- Updated the example to reflect the new build system
- Updated documentation

## [1.0.0] - 2016-04-05
### Added
- Initial Example
3 changes: 2 additions & 1 deletion Makefile.bf → Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@
################################################################################

PARENT_SUBDIRS += vcpu_factory_msr_bitmap
PARENT_SUBDIRS += vmcs_msr_bitmap

################################################################################
# Common
################################################################################

include %HYPER_ABS%/common/common_subdir.mk
include ../common/common_subdir.mk
52 changes: 24 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,50 +1,46 @@
# Bareflank Hypervisor MSR Bitmap Example
# Bareflank Hypervisor Example

## Description

This example demonstrates how to extend the Bareflank hypervisor to use
Intel's MSR Bitmaps to increase performance. For more information on how
Bareflank extensions work, please see the following:
This example demonstrates how to extend the Bareflank hypervisor to utilize the MSR bitmap feature of Intel virtualization. For more information on how Bareflank extensions work, please see the following:

[API Documentation](http://bareflank.github.io/hypervisor/html/)

## Compilation / Usage

To setup our extension, we can either clone the extension into the Bareflank
root folder and run make, or we can use the configure script to create an
out-of-tree build environment that has our extension setup for easy development.
Note that using the later approach, we can have more than one build
environment (the following assumes this is running on Linux).
First, you must clone the repo into your existing Bareflank repo. To see instructions on how to setup Bareflank, please see the following:

```
cd ~/
git clone https://github.com/Bareflank/hypervisor.git
git clone https://github.com/Bareflank/hypervisor_example_msr_bitmap.git
cd ~/hypervisor
[Bareflank Hypervisor](https://github.com/Bareflank/hypervisor)

./tools/scripts/setup-<xxx>.sh --no-configure
sudo reboot
At the moment, out-of-tree compilation is not supported.

cd ~/
mkdir build
cd ~/build
```
cd ~/hypervisor
git clone https://github.com/Bareflank/hypervisor_example_msr_bitmap
```

~/hypervisor/configure -m ~/hypervisor_example_msr_bitmap/bin/msr_bitmap.modules -e ~/hypervisor_example_msr_bitmap
Once the example repo is cloned, you can now build the example. Bareflank automatically looks for the examples, or any folder that starts with "src_", and builds these folders along with Bareflank itself.

```
make
make unittest
```

To test out our extended version of Bareflank, all we need to do is run the
make shortcuts as usual:
Finally, you can run the example. This can be done by running bfm manually, and providing the path to your custom modules list:

```
make linux_load
make quick
pushd bfm/bin/native
sudo LD_LIBRARY_PATH=. ./bfm load hypervisor_example_msr_bitmap/bin/msr_bitmap.modules
sudo LD_LIBRARY_PATH=. ./bfm start
sudo LD_LIBRARY_PATH=. ./bfm status
sudo LD_LIBRARY_PATH=. ./bfm dump
popd
```

or you can use the shortcuts:

```
make load MODULES=hypervisor_example_msr_bitmap/bin/msr_bitmap.modules
make start
make status
make dump
make stop
make linux_unload
```
27 changes: 14 additions & 13 deletions bin/msr_bitmap.modules
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,21 @@
#
# Note: The existing vcpu_factory module is commented out as we will be
# providing our own.
%BUILD_ABS%/sysroot/x86_64-elf/lib/libc++.so
%BUILD_ABS%/makefiles/bfvmm/src/debug_ring/bin/cross/libdebug_ring.so
%BUILD_ABS%/makefiles/bfvmm/src/entry/bin/cross/libentry.so
%BUILD_ABS%/makefiles/bfvmm/src/exit_handler/bin/cross/libexit_handler.so
%BUILD_ABS%/makefiles/bfvmm/src/intrinsics/bin/cross/libintrinsics.so
%BUILD_ABS%/makefiles/bfvmm/src/memory_manager/bin/cross/libmemory_manager.so
%BUILD_ABS%/makefiles/bfvmm/src/misc/bin/cross/libmisc.so
%BUILD_ABS%/makefiles/bfvmm/src/serial/bin/cross/libserial.so
%BUILD_ABS%/makefiles/bfvmm/src/vcpu/bin/cross/libvcpu.so
# %BUILD_ABS%/makefiles/bfvmm/src/vcpu_factory/bin/cross/libvcpu_factory.so
%BUILD_ABS%/makefiles/bfvmm/src/vmcs/bin/cross/libvmcs.so
%BUILD_ABS%/makefiles/bfvmm/src/vmxon/bin/cross/libvmxon.so
../../../bfvmm/bin/cross/libmemory_manager.so
../../../bfvmm/bin/cross/libentry.so
../../../bfvmm/bin/cross/libserial.so
../../../bfvmm/bin/cross/libdebug_ring.so
../../../bfvmm/bin/cross/libintrinsics.so
../../../bfvmm/bin/cross/libvmxon.so
../../../bfvmm/bin/cross/libvmcs.so
../../../bfvmm/bin/cross/libvcpu.so
#../../../bfvmm/bin/cross/libvcpu_factory.so
../../../bfvmm/bin/cross/libexit_handler.so
../../../bfvmm/bin/cross/libmisc.so
../../../bfvmm/bin/cross/libc++.so

# Custom Modules
#
# Note: This is where we provide our own vcpu_factory.
%BUILD_ABS%/makefiles/hypervisor_example_msr_bitmap/vcpu_factory_msr_bitmap/bin/cross/libvcpu_factory_msr_bitmap.so
../../../hypervisor_example_msr_bitmap/bin/cross/libvcpu_factory_msr_bitmap.so
../../../hypervisor_example_msr_bitmap/bin/cross/libvmcs_intel_x64_msr_bitmap.so
5 changes: 0 additions & 5 deletions exit_handler_msr_bitmap/exit_handler_msr_bitmap.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

#ifndef EXIT_HANDLER_MSR_BITMAP_H
#define EXIT_HANDLER_MSR_BITMAP_H

#include <exit_handler/exit_handler_intel_x64.h>

class exit_handler_msr_bitmap : public exit_handler_intel_x64
Expand All @@ -46,5 +43,3 @@ class exit_handler_msr_bitmap : public exit_handler_intel_x64
virtual void handle_wrmsr()
{ unimplemented_handler(); }
};

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@ SUBDIRS += src
# Common
################################################################################

include %HYPER_ABS%/common/common_subdir.mk
include ../../common/common_subdir.mk
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,20 @@ CROSS_DEFINES+=
# Output
################################################################################

CROSS_OBJDIR+=%BUILD_REL%/.build
CROSS_OUTDIR+=%BUILD_REL%/../bin
CROSS_OBJDIR:=.build
CROSS_OUTDIR:=../../bin

################################################################################
# Sources
################################################################################

SOURCES+=vcpu_factory_msr_bitmap.cpp

INCLUDE_PATHS+=./
INCLUDE_PATHS+=../../
INCLUDE_PATHS+=%HYPER_ABS%/include/
INCLUDE_PATHS+=%HYPER_ABS%/bfvmm/include/
INCLUDE_PATHS+=../../vmcs_msr_bitmap
INCLUDE_PATHS+=../../../include/
INCLUDE_PATHS+=../../../bfvmm/include/

LIBS+=

Expand Down Expand Up @@ -82,4 +84,4 @@ LINUX_LIBRARY_PATHS+=
# Common
################################################################################

include %HYPER_ABS%/common/common_target.mk
include ../../../common/common_target.mk
21 changes: 17 additions & 4 deletions vcpu_factory_msr_bitmap/src/vcpu_factory_msr_bitmap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,30 @@

#include <vcpu/vcpu_factory.h>
#include <vcpu/vcpu_intel_x64.h>
#include <vmcs_msr_bitmap/vmcs_intel_x64_msr_bitmap.h>
#include <vmcs_intel_x64_msr_bitmap.h>
#include <exit_handler_msr_bitmap/exit_handler_msr_bitmap.h>

std::shared_ptr<vcpu>
vcpu_factory::make_vcpu(int64_t vcpuid)
{
auto vmcs = std::make_shared<vmcs_intel_x64_msr_bitmap>();
// The vCPU Factory is used by the vCPU Manager to create vCPUs. This not
// only provides a hook for unit testing (the classic factory pattern), but
// also provides a clever place to hook in custom functionality above and
// beyond what bareflank provides by default.

// In this example, we are providing a custom exit handler that does not
// have any MSR emulation included.
auto exit_handler = std::make_shared<exit_handler_msr_bitmap>();

// Return a vCPU with our custom objects instead of the defaults which
// are represented by the null pointers.
// Provide a custom VMCS that initializes the MSR bitmap
auto vmcs = std::make_shared<vmcs_intel_x64_msr_bitmap>();

// Now that we have our custom exit handler, we can provide it to the
// vCPU that bareflank provides. Note that you could create your own vCPU
// if you need to, but in general, the existing vCPU should work for most
// cases as it allows you to override each of the classes that are required.
// In all cases here, if nullptr is provided, the code under the hood will
// create the class for you using the defaults.
return std::make_shared<vcpu_intel_x64>(vcpuid,
nullptr,
nullptr,
Expand Down
87 changes: 87 additions & 0 deletions vmcs_msr_bitmap/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
#
# Bareflank Hypervisor Examples
#
# Copyright (C) 2015 Assured Information Security, Inc.
# Author: Rian Quinn <[email protected]>
# Author: Brendan Kerrigan <[email protected]>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library 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
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

################################################################################
# Target Information
################################################################################

TARGET_NAME:=vmcs_intel_x64_msr_bitmap
TARGET_TYPE:=lib
TARGET_COMPILER:=cross

################################################################################
# Compiler Flags
################################################################################

CROSS_CCFLAGS+=
CROSS_CXXFLAGS+=
CROSS_ASMFLAGS+=
CROSS_LDFLAGS+=
CROSS_ARFLAGS+=
CROSS_DEFINES+=

################################################################################
# Output
################################################################################

CROSS_OBJDIR:=.build
CROSS_OUTDIR:=../bin

################################################################################
# Sources
################################################################################

SOURCES+=vmcs_intel_x64_msr_bitmap.cpp
SOURCES+=bitmap.cpp

INCLUDE_PATHS+=./
INCLUDE_PATHS+=../
INCLUDE_PATHS+=../../include/
INCLUDE_PATHS+=../../bfvmm/include/

LIBS+=

LIBRARY_PATHS+=

################################################################################
# Environment Specific
################################################################################

VMM_SOURCES+=
VMM_INCLUDE_PATHS+=
VMM_LIBS+=
VMM_LIBRARY_PATHS+=

WINDOWS_SOURCES+=
WINDOWS_INCLUDE_PATHS+=
WINDOWS_LIBS+=
WINDOWS_LIBRARY_PATHS+=

LINUX_SOURCES+=
LINUX_INCLUDE_PATHS+=
LINUX_LIBS+=
LINUX_LIBRARY_PATHS+=

################################################################################
# Common
################################################################################

include ../../common/common_target.mk
79 changes: 79 additions & 0 deletions vmcs_msr_bitmap/bitmap.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
//
// Bareflank Hypervisor
//
// Copyright (C) 2015 Assured Information Security, Inc.
// Author: Rian Quinn <[email protected]>
// Author: Brendan Kerrigan <[email protected]>
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library 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
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

#include <bitmap.h>
#include <memory_manager/memory_manager.h>

bitmap::bitmap(uint32_t num_bits)
{
m_length = num_bits >> 3;

if (num_bits & 7)
m_length++;

m_bitmap = std::make_unique<uint8_t[]>(m_length);

m_virt_addr = (uint64_t)m_bitmap.get();
m_phys_addr = (uint64_t)g_mm->virt_to_phys(m_bitmap.get());
}

bitmap::bitmap()
{
// Default to a full page bitmap
uint32_t num_bits = 4096*8;
m_length = num_bits >> 3;

if (num_bits & 7)
m_length++;

m_bitmap = std::make_unique<uint8_t[]>(m_length);

m_virt_addr = (uint64_t)m_bitmap.get();
m_phys_addr = (uint64_t)g_mm->virt_to_phys(m_bitmap.get());
}


void bitmap::set_bit(uint32_t n) noexcept
{
if ((n >> 3) > m_length)
return;

m_bitmap.get()[n >> 3] |= (1 << (n & 7));
}

void bitmap::clear_bit(uint32_t n) noexcept
{
if ((n >> 3) > m_length)
return;

m_bitmap.get()[n >> 3] &= ~(1 << (n & 7));
}

bool bitmap::bit(uint32_t n) const noexcept
{
if ((n >> 3) > m_length)
return false;

if (m_bitmap.get()[n >> 3] & (1 << (n & 7)))
return true;

return false;
}
Loading

0 comments on commit 10d61d4

Please sign in to comment.