summaryrefslogtreecommitdiffstats
path: root/src/kernel/basesegment.C
blob: 2dc99d4586a628118f18c4ca05ff2a71c1d1f417 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
//  IBM_PROLOG_BEGIN_TAG
//  This is an automatically generated prolog.
//
//  $Source: src/kernel/basesegment.C $
//
//  IBM CONFIDENTIAL
//
//  COPYRIGHT International Business Machines Corp. 2011
//
//  p1
//
//  Object Code Only (OCO) source materials
//  Licensed Internal Code Source Materials
//  IBM HostBoot Licensed Internal Code
//
//  The source code for this program is not published or other-
//  wise divested of its trade secrets, irrespective of what has
//  been deposited with the U.S. Copyright Office.
//
//  Origin: 30
//
//  IBM_PROLOG_END
#include <limits.h>
#include <errno.h>
#include <util/singleton.H>

#include <kernel/basesegment.H>
#include <kernel/segmentmgr.H>
#include <kernel/block.H>
#include <kernel/vmmmgr.H>
#include <kernel/cpuid.H>
//#include <kernel/console.H>

#define SLBE_s 40

BaseSegment::~BaseSegment()
{
    delete iv_block;
}

void BaseSegment::init()
{
    Singleton<BaseSegment>::instance()._init();
}

void BaseSegment::_init()
{
    // Assign segment to segment manager.
    SegmentManager::addSegment(this, SegmentManager::BASE_SEGMENT_ID);

    // Create initial static 3 or 8MB block.
    switch (CpuID::getCpuType())
    {
        case CORE_POWER7:
        case CORE_POWER7_PLUS:
        case CORE_POWER8_VENICE:
            iv_physMemSize = VmmManager::EIGHT_MEG;
            break;

        case CORE_POWER8_SALERNO:
        default:
            iv_physMemSize = VmmManager::FOUR_MEG;
            break;
    }
    // Base block is L3 cache physical memory size
    iv_block = new Block(0x0, iv_physMemSize);
    iv_block->setParent(this);

    // TODO iv_physMemSize needs to be recalculated when DIMM memory is avail.

    // Set default page permissions on block.
    for (uint64_t i = 0; i < 0x800000; i += PAGESIZE)
    {
            // External address filled in by linker as start of kernel's
            // data pages.
        extern void* data_load_address;

        // Don't map in the 0 (NULL) page.
        if (i == 0) continue;

        // Set pages in kernel text section to be read-only / executable.
        if (((uint64_t)&data_load_address) > i)
        {
            iv_block->setPhysicalPage(i, i, VmmManager::RO_EXE_ACCESS);
        }
        // Set all other pages to initially be read/write.  VFS will set
        // permissions on pages outside kernel.
        // (@TODO: Future Sprint, for now keep NORMAL_ACCESS as RWX, not RW.)
        else
        {
            iv_block->setPhysicalPage(i, i, VmmManager::NORMAL_ACCESS);
        }
    }
}

bool BaseSegment::handlePageFault(task_t* i_task, uint64_t i_addr)
{
    // Tail recursion to block chain.
    return iv_block->handlePageFault(i_task, i_addr);
}

/**
 * STATIC
 * Allocates a block of virtual memory of the given size
 */
int BaseSegment::mmAllocBlock(MessageQueue* i_mq,void* i_va,uint64_t i_size)
{
    return Singleton<BaseSegment>::instance()._mmAllocBlock(i_mq,i_va,i_size);
}

/**
 * Allocates a block of virtual memory of the given size
 */
int BaseSegment::_mmAllocBlock(MessageQueue* i_mq,void* i_va,uint64_t i_size)
{
    uint64_t l_vaddr = reinterpret_cast<uint64_t>(i_va);
    uint64_t l_blockSizeTotal = 0;
    iv_block->totalBlocksAlloc(l_blockSizeTotal);
    //Verify input address and size falls within this segment's address range
    if ((l_vaddr < this->getBaseAddress() ||
        l_vaddr >= (this->getBaseAddress() + (1ull << SLBE_s))) &&
        (l_blockSizeTotal+i_size <= (1ull << SLBE_s)))
    {
        return -1;
    }
    //TODO - Align i_size to page size
    Block* l_block = new Block(l_vaddr, i_size, i_mq);
    l_block->setParent(this);
    iv_block->appendBlock(l_block);
    return 0;
}

uint64_t BaseSegment::findPhysicalAddress(uint64_t i_vaddr) const
{
    if(i_vaddr < iv_physMemSize) return i_vaddr;
    return (iv_block ? iv_block->findPhysicalAddress(i_vaddr) : -EFAULT);
}

OpenPOWER on IntegriCloud