Skip to content

Commit fc1d90d

Browse files
committed
Merge branch 'move' of github.com:fwsGonzo/LiveUpdate into dev
2 parents c3741ba + 836da1a commit fc1d90d

29 files changed

Lines changed: 2426 additions & 0 deletions

lib/LiveUpdate/CMakeLists.txt

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
cmake_minimum_required(VERSION 2.8.9)
2+
# default IncludeOS location #FIXME#
3+
if (NOT DEFINED ENV{INCLUDEOS_PREFIX})
4+
set(ENV{INCLUDEOS_PREFIX} /usr/local)
5+
endif()
6+
include($ENV{INCLUDEOS_PREFIX}/includeos/pre.service.cmake)
7+
project(service)
8+
9+
set(SERVICE_NAME "Live Update")
10+
set(BINARY "LiveUpdate")
11+
set(SOURCES
12+
service.cpp test_boot.cpp test_all.cpp test_tcp.cpp
13+
)
14+
set(LOCAL_INCLUDES ".")
15+
16+
set(LIBRARIES ${CMAKE_BINARY_DIR}/libliveupdate.a)
17+
18+
set(DRIVERS
19+
virtionet
20+
#vmxnet3
21+
#solo5net
22+
#boot_logger
23+
#heap_debugging
24+
)
25+
26+
# include service build script
27+
include($ENV{INCLUDEOS_PREFIX}/includeos/post.service.cmake)
28+
29+
add_custom_command(
30+
OUTPUT hotswap64.bin
31+
COMMAND ${CMAKE_ASM_NASM_COMPILER} -f bin -o hotswap64.bin ${CMAKE_CURRENT_SOURCE_DIR}/hotswap64.asm
32+
DEPENDS hotswap64.asm
33+
)
34+
add_custom_target(hotswap64 DEPENDS hotswap64.bin)
35+
36+
# LiveUpdate static library
37+
add_library(liveupdate STATIC
38+
storage.cpp update.cpp resume.cpp rollback.cpp hotswap.cpp
39+
serialize_tcp.cpp hotswap64_blob.asm
40+
)
41+
add_dependencies(liveupdate hotswap64)
42+
target_link_libraries(service liveupdate)
43+
install(TARGETS liveupdate DESTINATION lib)
44+
45+
# Uncomment this to build vanilla:
46+
#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -msse4.2")

lib/LiveUpdate/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# LiveUpdate

lib/LiveUpdate/b.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/bash
2+
boot -b . && qemu-img convert -O vmdk build/LiveUpdate.img build/LiveUpdate.vmdk
3+
vmrun start vmx/Other\ 64-bit\ \(2\).vmx

lib/LiveUpdate/build_verify.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/bin/bash
2+
set -e
3+
clang++-3.8 -std=c++11 verify.cpp -I../IncludeOS/api -o verify
4+
./verify
5+
rm -f verify

lib/LiveUpdate/common.hpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#pragma once
2+
#include "hw_timer.hpp"
3+
#include <kernel/os.hpp>
4+
5+
//static void* LIVEUPD_LOCATION = (void*) 0x20000000; // at 512mb
6+
static void* LIVEUPD_LOCATION = (void*) 0x8000000; // at 128mb
7+
static void* SIZE_LOCATION = (void*) 0x7800000; // size
8+
static void* DATA_LOCATION = (void*) 0x7800008; // data
9+
extern char* heap_begin;
10+
extern char* heap_end;
11+
12+
inline void show_heap_stats()
13+
{
14+
ptrdiff_t heap_total = OS::heap_max() - (uintptr_t) heap_begin;
15+
double total = (heap_end - heap_begin) / (double) heap_total;
16+
17+
fprintf(stderr, "\tHeap is at: %p / %p (diff=%#x)\n",
18+
heap_end, (void*) OS::heap_max(),
19+
(uint32_t) (OS::heap_max() - (uintptr_t) heap_end));
20+
fprintf(stderr, "\tHeap usage: %u / %u Kb (%.2f%%)\n",
21+
(uint32_t) (heap_end - heap_begin) / 1024,
22+
heap_total / 1024, total * 100.0);
23+
}

lib/LiveUpdate/hotswap.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* Master thesis
3+
* by Alf-Andre Walla 2016-2017
4+
*
5+
**/
6+
asm(".org 0x8000");
7+
#define SOFT_RESET_MAGIC 0xFEE1DEAD
8+
9+
extern "C" __attribute__((noreturn))
10+
void hotswap(const char* base, int len, char* dest, void* start, void* reset_data)
11+
{
12+
// replace old kernel with new
13+
for (int i = 0; i < len; i++)
14+
dest[i] = base[i];
15+
// jump to _start
16+
asm volatile("jmp *%2" : : "a" (SOFT_RESET_MAGIC), "b" (reset_data), "c" (start));
17+
asm volatile(
18+
".global __hotswap_length;\n"
19+
"__hotswap_length:" );
20+
// we can never get here!
21+
__builtin_unreachable();
22+
}

lib/LiveUpdate/hotswap64.asm

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
;
2+
; Master thesis
3+
; by Alf-Andre Walla 2016-2017
4+
;
5+
;
6+
ORG 0x8000
7+
8+
%define code32_segment 0x08
9+
%define data32_segment 0x10
10+
%define SOFT_RESET_MAGIC 0xFEE1DEAD
11+
12+
[BITS 64]
13+
ALIGN 16
14+
;; first six pointer arguments are passed in
15+
;; RDI, RSI, RDX, RCX, R8, and R9
16+
;; hotswap64(
17+
;; RDI: char* dest,
18+
;; RSI: const char* base,
19+
;; RDX: size_t len,
20+
;; RCX: void* entry_function,
21+
;; R8: void* reset_data)
22+
hotswap_amd64:
23+
;; save soft reset data location and entry function
24+
mov rax, r8
25+
mov [startaddr], ecx ; rcx
26+
mov [bootaddr], eax ; r8
27+
28+
;; hotswap 64-bit kernel
29+
;; source: RSI
30+
;; dest: RDI
31+
mov rcx, rdx ;; count
32+
cld
33+
rep movsb
34+
35+
begin_enter_protected:
36+
; load 64-bit GDTR with 32-bit entries
37+
lgdt [gdtr64]
38+
; enter compatibility mode
39+
push data32_segment
40+
push rsp
41+
pushf
42+
push code32_segment
43+
mov ecx, compatibility_mode
44+
push rcx
45+
iretq
46+
47+
startaddr: dd 0
48+
bootaddr: dd 0
49+
50+
[BITS 32]
51+
ALIGN 16
52+
compatibility_mode:
53+
; disable paging
54+
mov ecx, cr0
55+
and ecx, 0x7fffffff ;; clear PG (bit 31)
56+
mov cr0, ecx
57+
; disable LM
58+
mov ecx, 0xC0000080 ; EFER MSR
59+
rdmsr
60+
and eax, ~(1 << 8) ; remove LM-bit
61+
wrmsr
62+
63+
;; enter 32-bit protected mode
64+
jmp code32_segment:protected_mode
65+
protected_mode:
66+
mov cx, data32_segment
67+
mov ss, cx
68+
mov ds, cx
69+
mov es, cx
70+
mov fs, cx
71+
mov gs, cx
72+
73+
;;rdtsc
74+
;;mov DWORD [0x10000], eax
75+
;;mov DWORD [0x10004], edx
76+
77+
;; enter the new service from its entry point
78+
;; in 32-bit protected mode, while passing
79+
;; multiboot parameters in eax and ebx
80+
mov eax, SOFT_RESET_MAGIC
81+
mov ebx, [bootaddr]
82+
jmp DWORD [startaddr]
83+
84+
gdtr:
85+
dw gdt32_end - gdt32 - 1
86+
dd gdt32
87+
gdt32:
88+
;; Entry 0x0: Null descriptor
89+
dq 0x0
90+
;; Entry 0x18: 32-bit Code segment
91+
dw 0xffff ;Limit
92+
dw 0x0000 ;Base 15:00
93+
db 0x00 ;Base 23:16
94+
dw 0xcf9a ;Flags / Limit / Type [F,L,F,Type]
95+
db 0x00 ;Base 32:24
96+
;; Entry 0x20: 32-bit Data segment
97+
dw 0xffff ;Limit
98+
dw 0x0000 ;Base 15:00
99+
db 0x00 ;Base 23:16
100+
dw 0xcf92 ;Flags / Limit / Type [F,L,F,Type]
101+
db 0x00 ;Base 32:24
102+
gdt32_end:
103+
gdtr64:
104+
dw $ - gdt32 - 1 ; Limit
105+
dq gdt32 ; Base

lib/LiveUpdate/hotswap64_blob.asm

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
global hotswap64
2+
global hotswap64_len
3+
4+
SECTION .text
5+
hotswap64:
6+
incbin "hotswap64.bin"
7+
8+
hotswap64_len:
9+
dd $ - hotswap64

lib/LiveUpdate/hw_timer.hpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* Master thesis
3+
* by Alf-Andre Walla 2016-2017
4+
*
5+
**/
6+
#pragma once
7+
8+
#include <kernel/os.hpp>
9+
#include <cstdint>
10+
#include <cstdio>
11+
12+
struct HW_timer
13+
{
14+
HW_timer(const char* ctx)
15+
: context(ctx), time(OS::cycles_since_boot()) {}
16+
~HW_timer() {
17+
const auto diff = OS::cycles_since_boot() - time;
18+
const double div = OS::cpu_freq().count() * 1000.0;
19+
const double time = diff / div;
20+
21+
printf("HW timer for %s: %lld (%.2f ms)\n",
22+
context, diff, time);
23+
}
24+
private:
25+
const char* context;
26+
int64_t time;
27+
};

lib/LiveUpdate/install.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/bash
2+
boot . -b
3+
cp build/libliveupdate.a $INCLUDEOS_PREFIX/includeos/x86_64/lib

0 commit comments

Comments
 (0)