Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

exercises/bl4ckout31: add lesson03 answers #72

Merged
merged 1 commit into from
Jul 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions exercises/lesson03/1/bl4ckout31/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
ARMGNU ?= aarch64-linux-gnu

COPS = -Wall -nostdlib -nostartfiles -ffreestanding -Iinclude -mgeneral-regs-only
ASMOPS = -Iinclude

BUILD_DIR = build
SRC_DIR = src

all : kernel8.img

clean :
rm -rf $(BUILD_DIR) *.img

$(BUILD_DIR)/%_c.o: $(SRC_DIR)/%.c
mkdir -p $(@D)
$(ARMGNU)-gcc $(COPS) -MMD -c $< -o $@

$(BUILD_DIR)/%_s.o: $(SRC_DIR)/%.S
$(ARMGNU)-gcc $(ASMOPS) -MMD -c $< -o $@

C_FILES = $(wildcard $(SRC_DIR)/*.c)
ASM_FILES = $(wildcard $(SRC_DIR)/*.S)
OBJ_FILES = $(C_FILES:$(SRC_DIR)/%.c=$(BUILD_DIR)/%_c.o)
OBJ_FILES += $(ASM_FILES:$(SRC_DIR)/%.S=$(BUILD_DIR)/%_s.o)

DEP_FILES = $(OBJ_FILES:%.o=%.d)
-include $(DEP_FILES)

kernel8.img: $(SRC_DIR)/linker.ld $(OBJ_FILES)
$(ARMGNU)-ld -T $(SRC_DIR)/linker.ld -o $(BUILD_DIR)/kernel8.elf $(OBJ_FILES)
$(ARMGNU)-objcopy $(BUILD_DIR)/kernel8.elf -O binary kernel8.img
1 change: 1 addition & 0 deletions exercises/lesson03/1/bl4ckout31/build.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
docker run --rm -v %cd%:/app -w /app smatyukevich/raspberry-pi-os-builder make %1
3 changes: 3 additions & 0 deletions exercises/lesson03/1/bl4ckout31/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash

docker run --rm -v $(pwd):/app -w /app smatyukevich/raspberry-pi-os-builder make $1
42 changes: 42 additions & 0 deletions exercises/lesson03/1/bl4ckout31/include/arm/sysregs.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#ifndef _SYSREGS_H
#define _SYSREGS_H

// ***************************************
// SCTLR_EL1, System Control Register (EL1), Page 2654 of AArch64-Reference-Manual.
// ***************************************

#define SCTLR_RESERVED (3 << 28) | (3 << 22) | (1 << 20) | (1 << 11)
#define SCTLR_EE_LITTLE_ENDIAN (0 << 25)
#define SCTLR_EOE_LITTLE_ENDIAN (0 << 24)
#define SCTLR_I_CACHE_DISABLED (0 << 12)
#define SCTLR_D_CACHE_DISABLED (0 << 2)
#define SCTLR_MMU_DISABLED (0 << 0)
#define SCTLR_MMU_ENABLED (1 << 0)

#define SCTLR_VALUE_MMU_DISABLED (SCTLR_RESERVED | SCTLR_EE_LITTLE_ENDIAN | SCTLR_I_CACHE_DISABLED | SCTLR_D_CACHE_DISABLED | SCTLR_MMU_DISABLED)

// ***************************************
// HCR_EL2, Hypervisor Configuration Register (EL2), Page 2487 of AArch64-Reference-Manual.
// ***************************************

#define HCR_RW (1 << 31)
#define HCR_VALUE HCR_RW

// ***************************************
// SCR_EL3, Secure Configuration Register (EL3), Page 2648 of AArch64-Reference-Manual.
// ***************************************

#define SCR_RESERVED (3 << 4)
#define SCR_RW (1 << 10)
#define SCR_NS (1 << 0)
#define SCR_VALUE (SCR_RESERVED | SCR_RW | SCR_NS)

// ***************************************
// SPSR_EL3, Saved Program Status Register (EL3) Page 389 of AArch64-Reference-Manual.
// ***************************************

#define SPSR_MASK_ALL (7 << 6)
#define SPSR_EL1h (5 << 0)
#define SPSR_VALUE (SPSR_MASK_ALL | SPSR_EL1h)

#endif
26 changes: 26 additions & 0 deletions exercises/lesson03/1/bl4ckout31/include/entry.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#ifndef _ENTRY_H
#define _ENTRY_H

#define S_FRAME_SIZE 256 // size of all saved registers

#define SYNC_INVALID_EL1t 0
#define IRQ_INVALID_EL1t 1
#define FIQ_INVALID_EL1t 2
#define ERROR_INVALID_EL1t 3

#define SYNC_INVALID_EL1h 4
#define IRQ_INVALID_EL1h 5
#define FIQ_INVALID_EL1h 6
#define ERROR_INVALID_EL1h 7

#define SYNC_INVALID_EL0_64 8
#define IRQ_INVALID_EL0_64 9
#define FIQ_INVALID_EL0_64 10
#define ERROR_INVALID_EL0_64 11

#define SYNC_INVALID_EL0_32 12
#define IRQ_INVALID_EL0_32 13
#define FIQ_INVALID_EL0_32 14
#define ERROR_INVALID_EL0_32 15

#endif
10 changes: 10 additions & 0 deletions exercises/lesson03/1/bl4ckout31/include/irq.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#ifndef _IRQ_H
#define _IRQ_H

void enable_interrupt_controller( void );

void irq_vector_init( void );
void enable_irq( void );
void disable_irq( void );

#endif /*_IRQ_H */
9 changes: 9 additions & 0 deletions exercises/lesson03/1/bl4ckout31/include/mini_uart.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#ifndef _MINI_UART_H
#define _MINI_UART_H

void uart_init ( void );
char uart_recv ( void );
void uart_send ( char c );
void putc ( void* p, char c );

#endif /*_MINI_UART_H */
19 changes: 19 additions & 0 deletions exercises/lesson03/1/bl4ckout31/include/mm.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#ifndef _MM_H
#define _MM_H

#define PAGE_SHIFT 12
#define TABLE_SHIFT 9
#define SECTION_SHIFT (PAGE_SHIFT + TABLE_SHIFT)

#define PAGE_SIZE (1 << PAGE_SHIFT)
#define SECTION_SIZE (1 << SECTION_SHIFT)

#define LOW_MEMORY (2 * SECTION_SIZE)

#ifndef __ASSEMBLER__

void memzero(unsigned long src, unsigned long n);

#endif

#endif /*_MM_H */
7 changes: 7 additions & 0 deletions exercises/lesson03/1/bl4ckout31/include/peripherals/base.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#ifndef _P_BASE_H
#define _P_BASE_H

#define PBASE 0x3F000000
#define LPBASE 0x40000000

#endif /*_P_BASE_H */
12 changes: 12 additions & 0 deletions exercises/lesson03/1/bl4ckout31/include/peripherals/gpio.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#ifndef _P_GPIO_H
#define _P_GPIO_H

#include "peripherals/base.h"

#define GPFSEL1 (PBASE+0x00200004)
#define GPSET0 (PBASE+0x0020001C)
#define GPCLR0 (PBASE+0x00200028)
#define GPPUD (PBASE+0x00200094)
#define GPPUDCLK0 (PBASE+0x00200098)

#endif /*_P_GPIO_H */
28 changes: 28 additions & 0 deletions exercises/lesson03/1/bl4ckout31/include/peripherals/irq.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#ifndef _P_IRQ_H
#define _P_IRQ_H

#include "peripherals/base.h"

#define IRQ_BASIC_PENDING (PBASE+0x0000B200)
#define IRQ_PENDING_1 (PBASE+0x0000B204)
#define IRQ_PENDING_2 (PBASE+0x0000B208)
#define FIQ_CONTROL (PBASE+0x0000B20C)
#define ENABLE_IRQS_1 (PBASE+0x0000B210)
#define ENABLE_IRQS_2 (PBASE+0x0000B214)
#define ENABLE_BASIC_IRQS (PBASE+0x0000B218)
#define DISABLE_IRQS_1 (PBASE+0x0000B21C)
#define DISABLE_IRQS_2 (PBASE+0x0000B220)
#define DISABLE_BASIC_IRQS (PBASE+0x0000B224)

#define SYSTEM_TIMER_IRQ_0 (1 << 0)
#define SYSTEM_TIMER_IRQ_1 (1 << 1)
#define SYSTEM_TIMER_IRQ_2 (1 << 2)
#define SYSTEM_TIMER_IRQ_3 (1 << 3)

// See BCM2836 ARM-local peripherals at
// https://www.raspberrypi.org/documentation/hardware/raspberrypi/bcm2836/QA7_rev3.4.pdf

#define CORE0_INTERRUPT_SOURCES (LPBASE+0x60)
#define LTIMER_INTERRUPT (1 << 11)

#endif /*_P_IRQ_H */
19 changes: 19 additions & 0 deletions exercises/lesson03/1/bl4ckout31/include/peripherals/mini_uart.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#ifndef _P_MINI_UART_H
#define _P_MINI_UART_H

#include "peripherals/base.h"

#define AUX_ENABLES (PBASE+0x00215004)
#define AUX_MU_IO_REG (PBASE+0x00215040)
#define AUX_MU_IER_REG (PBASE+0x00215044)
#define AUX_MU_IIR_REG (PBASE+0x00215048)
#define AUX_MU_LCR_REG (PBASE+0x0021504C)
#define AUX_MU_MCR_REG (PBASE+0x00215050)
#define AUX_MU_LSR_REG (PBASE+0x00215054)
#define AUX_MU_MSR_REG (PBASE+0x00215058)
#define AUX_MU_SCRATCH (PBASE+0x0021505C)
#define AUX_MU_CNTL_REG (PBASE+0x00215060)
#define AUX_MU_STAT_REG (PBASE+0x00215064)
#define AUX_MU_BAUD_REG (PBASE+0x00215068)

#endif /*_P_MINI_UART_H */
31 changes: 31 additions & 0 deletions exercises/lesson03/1/bl4ckout31/include/peripherals/timer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#ifndef _P_TIMER_H
#define _P_TIMER_H

#include "peripherals/base.h"

#define TIMER_CS (PBASE+0x00003000)
#define TIMER_CLO (PBASE+0x00003004)
#define TIMER_CHI (PBASE+0x00003008)
#define TIMER_C0 (PBASE+0x0000300C)
#define TIMER_C1 (PBASE+0x00003010)
#define TIMER_C2 (PBASE+0x00003014)
#define TIMER_C3 (PBASE+0x00003018)

#define TIMER_CS_M0 (1 << 0)
#define TIMER_CS_M1 (1 << 1)
#define TIMER_CS_M2 (1 << 2)
#define TIMER_CS_M3 (1 << 3)

// See BCM2836 ARM-local peripherals at
// https://www.raspberrypi.org/documentation/hardware/raspberrypi/bcm2836/QA7_rev3.4.pdf

#define LTIMER_CTRL (LPBASE+0x34)
#define LTIMER_CLR (LPBASE+0x38)

#define LTIMER_CTRL_EN (1 << 28)
#define LTIMER_CTRL_INT_EN (1 << 29)
#define LTIMER_CTRL_VALUE (LTIMER_CTRL_EN | LTIMER_CTRL_INT_EN)

#define LTIMER_CLR_ACK (1 << 31)

#endif /*_P_TIMER_H */
106 changes: 106 additions & 0 deletions exercises/lesson03/1/bl4ckout31/include/printf.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*
File: printf.h

Copyright (C) 2004 Kustaa Nyholm

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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

This library is realy just two files: 'printf.h' and 'printf.c'.

They provide a simple and small (+200 loc) printf functionality to
be used in embedded systems.

I've found them so usefull in debugging that I do not bother with a
debugger at all.

They are distributed in source form, so to use them, just compile them
into your project.

Two printf variants are provided: printf and sprintf.

The formats supported by this implementation are: 'd' 'u' 'c' 's' 'x' 'X'.

Zero padding and field width are also supported.

If the library is compiled with 'PRINTF_SUPPORT_LONG' defined then the
long specifier is also
supported. Note that this will pull in some long math routines (pun intended!)
and thus make your executable noticably longer.

The memory foot print of course depends on the target cpu, compiler and
compiler options, but a rough guestimate (based on a H8S target) is about
1.4 kB for code and some twenty 'int's and 'char's, say 60 bytes of stack space.
Not too bad. Your milage may vary. By hacking the source code you can
get rid of some hunred bytes, I'm sure, but personally I feel the balance of
functionality and flexibility versus code size is close to optimal for
many embedded systems.

To use the printf you need to supply your own character output function,
something like :

void putc ( void* p, char c)
{
while (!SERIAL_PORT_EMPTY) ;
SERIAL_PORT_TX_REGISTER = c;
}

Before you can call printf you need to initialize it to use your
character output function with something like:

init_printf(NULL,putc);

Notice the 'NULL' in 'init_printf' and the parameter 'void* p' in 'putc',
the NULL (or any pointer) you pass into the 'init_printf' will eventually be
passed to your 'putc' routine. This allows you to pass some storage space (or
anything realy) to the character output function, if necessary.
This is not often needed but it was implemented like that because it made
implementing the sprintf function so neat (look at the source code).

The code is re-entrant, except for the 'init_printf' function, so it
is safe to call it from interupts too, although this may result in mixed output.
If you rely on re-entrancy, take care that your 'putc' function is re-entrant!

The printf and sprintf functions are actually macros that translate to
'tfp_printf' and 'tfp_sprintf'. This makes it possible
to use them along with 'stdio.h' printf's in a single source file.
You just need to undef the names before you include the 'stdio.h'.
Note that these are not function like macros, so if you have variables
or struct members with these names, things will explode in your face.
Without variadic macros this is the best we can do to wrap these
fucnction. If it is a problem just give up the macros and use the
functions directly or rename them.

For further details see source code.

regs Kusti, 23.10.2004
*/


#ifndef __TFP_PRINTF__
#define __TFP_PRINTF__

#include <stdarg.h>

void init_printf(void* putp,void (*putf) (void*,char));

void tfp_printf(char *fmt, ...);
void tfp_sprintf(char* s,char *fmt, ...);

void tfp_format(void* putp,void (*putf) (void*,char),char *fmt, va_list va);

#define printf tfp_printf
#define sprintf tfp_sprintf

#endif
10 changes: 10 additions & 0 deletions exercises/lesson03/1/bl4ckout31/include/timer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#ifndef _TIMER_H
#define _TIMER_H

void timer_init ( void );
void handle_timer_irq ( void );

void local_timer_init ( void );
void handle_local_timer_irq ( void );

#endif /*_TIMER_H */
9 changes: 9 additions & 0 deletions exercises/lesson03/1/bl4ckout31/include/utils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#ifndef _UTILS_H
#define _UTILS_H

extern void delay ( unsigned long);
extern void put32 ( unsigned long, unsigned int );
extern unsigned int get32 ( unsigned long );
extern int get_el ( void );

#endif /*_UTILS_H */
Loading