starter_fs.py revision 12153
1# Copyright (c) 2016-2017 ARM Limited
2# All rights reserved.
3#
4# The license below extends only to copyright in the software and shall
5# not be construed as granting a license to any other intellectual
6# property including but not limited to intellectual property relating
7# to a hardware implementation of the functionality of the software
8# licensed hereunder.  You may use the software subject to the license
9# terms below provided that you ensure that this notice is replicated
10# unmodified and in its entirety in all distributions of the software,
11# modified or unmodified, in source code or in binary form.
12#
13# Redistribution and use in source and binary forms, with or without
14# modification, are permitted provided that the following conditions are
15# met: redistributions of source code must retain the above copyright
16# notice, this list of conditions and the following disclaimer;
17# redistributions in binary form must reproduce the above copyright
18# notice, this list of conditions and the following disclaimer in the
19# documentation and/or other materials provided with the distribution;
20# neither the name of the copyright holders nor the names of its
21# contributors may be used to endorse or promote products derived from
22# this software without specific prior written permission.
23#
24# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
27# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
28# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
30# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
34# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35#
36#  Authors:  Andreas Sandberg
37#            Chuan Zhu
38#            Gabor Dozsa
39#
40
41"""This script is the full system example script from the ARM
42Research Starter Kit on System Modeling. More information can be found
43at: http://www.arm.com/ResearchEnablement/SystemModeling
44"""
45
46import os
47import m5
48from m5.util import addToPath
49from m5.objects import *
50import argparse
51
52m5.util.addToPath('../..')
53
54from common import SysPaths
55from common import MemConfig
56from common.cores.arm import HPI
57
58import devices
59
60
61default_dist_version = '20170616'
62default_kernel = 'vmlinux.vexpress_gem5_v1_64.' + default_dist_version
63default_disk = 'linaro-minimal-aarch64.img'
64
65
66# Pre-defined CPU configurations. Each tuple must be ordered as : (cpu_class,
67# l1_icache_class, l1_dcache_class, walk_cache_class, l2_Cache_class). Any of
68# the cache class may be 'None' if the particular cache is not present.
69cpu_types = {
70
71    "atomic" : ( AtomicSimpleCPU, None, None, None, None),
72    "minor" : (MinorCPU,
73               devices.L1I, devices.L1D,
74               devices.WalkCache,
75               devices.L2),
76    "hpi" : ( HPI.HPI,
77              HPI.HPI_ICache, HPI.HPI_DCache,
78              HPI.HPI_WalkCache,
79              HPI.HPI_L2)
80}
81
82def create_cow_image(name):
83    """Helper function to create a Copy-on-Write disk image"""
84    image = CowDiskImage()
85    image.child.image_file = SysPaths.disk(name)
86
87    return image;
88
89
90def create(args):
91    ''' Create and configure the system object. '''
92
93    if not args.dtb:
94        dtb_file = SysPaths.binary("armv8_gem5_v1_%icpu.%s.dtb" %
95                                   (args.num_cores, default_dist_version))
96    else:
97        dtb_file = args.dtb
98
99    if args.script and not os.path.isfile(args.script):
100        print "Error: Bootscript %s does not exist" % args.script
101        sys.exit(1)
102
103    cpu_class = cpu_types[args.cpu][0]
104    mem_mode = cpu_class.memory_mode()
105    # Only simulate caches when using a timing CPU (e.g., the HPI model)
106    want_caches = True if mem_mode == "timing" else False
107
108    system = devices.SimpleSystem(want_caches,
109                                  args.mem_size,
110                                  mem_mode=mem_mode,
111                                  dtb_filename=dtb_file,
112                                  kernel=SysPaths.binary(args.kernel),
113                                  readfile=args.script)
114
115    MemConfig.config_mem(args, system)
116
117    # Add the PCI devices we need for this system. The base system
118    # doesn't have any PCI devices by default since they are assumed
119    # to be added by the configurastion scripts needin them.
120    system.pci_devices = [
121        # Create a VirtIO block device for the system's boot
122        # disk. Attach the disk image using gem5's Copy-on-Write
123        # functionality to avoid writing changes to the stored copy of
124        # the disk image.
125        PciVirtIO(vio=VirtIOBlock(image=create_cow_image(args.disk_image))),
126    ]
127
128    # Attach the PCI devices to the system. The helper method in the
129    # system assigns a unique PCI bus ID to each of the devices and
130    # connects them to the IO bus.
131    for dev in system.pci_devices:
132        system.attach_pci(dev)
133
134    # Wire up the system's memory system
135    system.connect()
136
137    # Add CPU clusters to the system
138    system.cpu_cluster = [
139        devices.CpuCluster(system,
140                           args.num_cores,
141                           args.cpu_freq, "1.0V",
142                           *cpu_types[args.cpu]),
143    ]
144
145    # Create a cache hierarchy for the cluster. We are assuming that
146    # clusters have core-private L1 caches and an L2 that's shared
147    # within the cluster.
148    for cluster in system.cpu_cluster:
149        system.addCaches(want_caches, last_cache_level=2)
150
151    # Setup gem5's minimal Linux boot loader.
152    system.realview.setupBootLoader(system.membus, system, SysPaths.binary)
153
154    # Linux boot command flags
155    kernel_cmd = [
156        # Tell Linux to use the simulated serial port as a console
157        "console=ttyAMA0",
158        # Hard-code timi
159        "lpj=19988480",
160        # Disable address space randomisation to get a consistent
161        # memory layout.
162        "norandmaps",
163        # Tell Linux where to find the root disk image.
164        "root=/dev/vda1",
165        # Mount the root disk read-write by default.
166        "rw",
167        # Tell Linux about the amount of physical memory present.
168        "mem=%s" % args.mem_size,
169    ]
170    system.boot_osflags = " ".join(kernel_cmd)
171
172    return system
173
174
175def run(args):
176    cptdir = m5.options.outdir
177    if args.checkpoint:
178        print "Checkpoint directory: %s" % cptdir
179
180    while True:
181        event = m5.simulate()
182        exit_msg = event.getCause()
183        if exit_msg == "checkpoint":
184            print "Dropping checkpoint at tick %d" % m5.curTick()
185            cpt_dir = os.path.join(m5.options.outdir, "cpt.%d" % m5.curTick())
186            m5.checkpoint(os.path.join(cpt_dir))
187            print "Checkpoint done."
188        else:
189            print exit_msg, " @ ", m5.curTick()
190            break
191
192    sys.exit(event.getCode())
193
194
195def main():
196    parser = argparse.ArgumentParser(epilog=__doc__)
197
198    parser.add_argument("--dtb", type=str, default=None,
199                        help="DTB file to load")
200    parser.add_argument("--kernel", type=str, default=default_kernel,
201                        help="Linux kernel")
202    parser.add_argument("--disk-image", type=str,
203                        default=default_disk,
204                        help="Disk to instantiate")
205    parser.add_argument("--script", type=str, default="",
206                        help = "Linux bootscript")
207    parser.add_argument("--cpu", type=str, choices=cpu_types.keys(),
208                        default="atomic",
209                        help="CPU model to use")
210    parser.add_argument("--cpu-freq", type=str, default="4GHz")
211    parser.add_argument("--num-cores", type=int, default=1,
212                        help="Number of CPU cores")
213    parser.add_argument("--mem-type", default="DDR3_1600_8x8",
214                        choices=MemConfig.mem_names(),
215                        help = "type of memory to use")
216    parser.add_argument("--mem-channels", type=int, default=1,
217                        help = "number of memory channels")
218    parser.add_argument("--mem-ranks", type=int, default=None,
219                        help = "number of memory ranks per channel")
220    parser.add_argument("--mem-size", action="store", type=str,
221                        default="2GB",
222                        help="Specify the physical memory size")
223    parser.add_argument("--checkpoint", action="store_true")
224    parser.add_argument("--restore", type=str, default=None)
225
226
227    args = parser.parse_args()
228
229    root = Root(full_system=True)
230    root.system = create(args)
231
232    if args.restore is not None:
233        m5.instantiate(args.restore)
234    else:
235        m5.instantiate()
236
237    run(args)
238
239
240if __name__ == "__m5_main__":
241    main()
242